add function to list all files in a given directory

This commit is contained in:
Alexander Weinhold 2016-11-19 00:55:13 +01:00
parent 457eec7add
commit 64972dd953

View File

@ -3,6 +3,7 @@ package goutil
import (
"io/ioutil"
"path/filepath"
"strings"
)
func WriteFile(filename string, data string) error {
@ -18,3 +19,19 @@ func TrimExt(path string) string {
extension := filepath.Ext(path)
return path[0 : len(path)-len(extension)]
}
func ListFilesExt(dir string, ext string) []string {
list := make([]string, 0)
ext = strings.ToUpper(ext)
files, err := ioutil.ReadDir(dir)
if err == nil {
for _, file := range files {
if !file.IsDir() {
if strings.ToUpper(filepath.Ext(file.Name())) == ext {
list = append(list, file.Name())
}
}
}
}
return list
}