Skip to content

Instantly share code, notes, and snippets.

@jangler
Created February 21, 2014 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jangler/9129084 to your computer and use it in GitHub Desktop.
Save jangler/9129084 to your computer and use it in GitHub Desktop.
simple regex-based recursive filename search
package main
import (
"io/ioutil"
"os"
"path"
"regexp"
)
func handle(err error) {
if err != nil {
panic(err)
}
}
func fileList(dirname string) []string {
contents, err := ioutil.ReadDir(dirname)
handle(err)
names := make([]string, len(contents))
for _, file := range contents {
if file.IsDir() {
names = append(names, fileList(path.Join(dirname, file.Name()))...)
} else {
names = append(names, path.Join(dirname, file.Name()))
}
}
return names
}
func main() {
dirname, err := os.Getwd()
handle(err)
filePaths := fileList(dirname)
for _, arg := range os.Args[1:] {
re, err := regexp.Compile(arg)
handle(err)
for _, filePath := range filePaths {
if re.MatchString(filePath) {
println(path.Clean(filePath))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment