Skip to content

Commit

Permalink
Excluding sub-directories from dir walker
Browse files Browse the repository at this point in the history
  • Loading branch information
MewX committed Dec 27, 2022
1 parent aa5341f commit e78b303
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,26 @@ func ReadEntireFile(fullPath string) string {

// GetFilePathListWithPattern returns the full paths for files matching the pattern in the base path.
func GetFilePathListWithPattern(basePath, fileNamePattern string) []string {
sepCount := strings.Count(basePath, string(os.PathSeparator))

var files []string
filepath.WalkDir(basePath, func(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}

// Skipping sub-directories.
if d.IsDir() && strings.Count(s, string(os.PathSeparator)) > sepCount {
log.Println("Skipping sub-directory:", s)
return fs.SkipDir
}

if matched, _ := filepath.Match(fileNamePattern, d.Name()); matched {
files = append(files, s)
}
return nil
})
log.Println("Found", len(files), "matched files with pattern:", fileNamePattern)
log.Println("Found", len(files), "matched files with pattern:", fileNamePattern, "in", basePath)
return files
}

Expand Down

0 comments on commit e78b303

Please sign in to comment.