Created
February 21, 2014 05:04
-
-
Save jangler/9129084 to your computer and use it in GitHub Desktop.
simple regex-based recursive filename search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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