Walk a directory and match globs
The examples on this page run in a directory that holds a project:
app/.env
app/.git/config
app/README.md
app/docs/guide.md
app/src/main.ant
app/src/util/strings.ant
app/src/util/strings_test.ant
app/tests/main_test.antFind files by pattern
This script lists every Ant file below app with its size:
let root = "app"
let sources = try File.glob("**/*.ant", dir: root)
for path in sources {
let info = try File.stat(Path.join([root, path]))
println("${path} ${info.size} bytes")
}src/main.ant 14 bytes
src/util/strings.ant 48 bytes
src/util/strings_test.ant 45 bytes
tests/main_test.ant 31 bytes- Pass the pattern and the directory to search to File.glob.
**matches zero or more directories, and*any run of characters within one name. - Join the directory and each path with Path.join.
File.globreturns paths relative todir:, in lexical order. - Use the joined path, here with File.stat.
The pattern syntax is that of Path.match. Wildcards skip names that begin with a dot, so the pattern never descends into .git. Nothing matching gives an empty array, not an error. A pattern has no {a,b} alternation: { matches itself, so *.{md,txt} matches only a name that ends in .{md,txt}. Matching is case-sensitive on every system, so *.TXT does not match a.txt.
Walk every file
File.walk returns every file below a directory, hidden ones included, as paths relative to it. It lists files, not directories:
for path in try File.walk("app") {
println(path)
}.env
.git/config
README.md
docs/guide.md
src/main.ant
src/util/strings.ant
src/util/strings_test.ant
tests/main_test.antFilter walked paths with a pattern
To match among paths you already have, or to include hidden files, filter File.walk with Path.match. Match each path against a pattern with Path.match. It returns Err only for a malformed pattern, so a fixed pattern can unwrap it:
let tests = try File.walk("app")
| Array.filter({ Path.match("**/*_test.ant", x).unwrap() })
println(tests)["src/util/strings_test.ant", "tests/main_test.ant"]To match the file name wherever the file is, match against Path.base of the path:
let docs = try File.walk("app")
| Array.filter({ Path.match("*.md", Path.base(x)).unwrap() })
println(docs)["README.md", "docs/guide.md"]To match several extensions, keep the patterns in an array and ask whether some of them matches. The outer callback is a function literal, so the inner lambda's x is the pattern:
let patterns = ["*.md", "*.ant"]
let found = try File.walk("app")
| Array.filter(func(path: string): bool {
patterns.some({ Path.match(x, Path.base(path)).unwrap() })
})
println(found)["README.md", "docs/guide.md", "src/main.ant", "src/util/strings.ant", "src/util/strings_test.ant", "tests/main_test.ant"]Walked paths include hidden ones, and wildcards skip them; pass hidden: true to match them too:
let configs = try File.walk("app")
| Array.filter({ Path.match("**/config", x, hidden: true).unwrap() })
println(configs)[".git/config"]Include hidden files
Pass hidden: true to let wildcards match names that begin with a dot. ** alone matches everything below the directory, directories included:
for path in try File.glob("**", dir: "app", hidden: true) {
println(path)
}.env
.git
.git/config
README.md
docs
docs/guide.md
src
src/main.ant
src/util
src/util/strings.ant
src/util/strings_test.ant
tests
tests/main_test.antFind directories
End the pattern with / to match directories only. Each path keeps the /:
println(try File.glob("**/", dir: "app"))["docs/", "src/", "src/util/", "tests/"]List one directory
To list only what is directly in a directory, with the kind of each entry, call File.readDir. It does not descend, and it includes hidden names:
for { name, isDir } in try File.readDir("app") {
println(if isDir { name + "/" } else { name })
}.env
.git/
README.md
docs/
src/
tests/The names are bare, so join one with Path.join(["app", name]) to use it. File.list gives the names alone.
Report a bad pattern
File.glob returns Err for a malformed pattern and for a dir: that is not a directory. Add the directory to the error with Result.wrapErr:
let root = "app"
let found = try (File.glob("[a-", dir: root) | Result.wrapErr("search ${root}"))
println(found)$ ant run find.ant; echo $?
search app: malformed pattern [a-: [ without ]
1A directory missing below dir:, as missing/*.txt asks for, is no match, not an error.
See also
File.glob · File.walk · File.list · File.readDir · File.stat · Path.match · Path.join · Path.base · Result.wrapErr · Path
