File.list
func File.list(dir: string): Result[Array[string], Error]File.list returns the names of the entries directly in dir, in lexical order, the order in which strings compare, so "10.txt" comes before "9.txt". It follows a link at dir. Unlike File.walk it does not descend, and it returns names rather than paths.
Details
- The names are bare: to read or stat an entry, join it to
dirwithPath.join. - Subdirectories, hidden names that begin with a dot, and symbolic links are listed like files.
- The names do not say what kind of entry each is. File.readDir returns the same entries in the same order, each with
isDirandisSymlink, with noFile.statper name. - An empty directory gives
Okwith an empty array. A missingdir, or adirthat is a file, isErr.
Basic examples
List the entries of a directory:
println(File.list("notes"))Ok{ value: ["archive", "ideas.md", "todo.txt"] }Unwrap the names with try and loop over them. In a script, try ends the script on an Err:
for name in try File.list("notes") {
println(name)
}archive
ideas.md
todo.txtScope
An empty directory gives an empty array:
try File.mkdir("empty")
println(File.list("empty"))Ok{ value: [] }Names that begin with a dot are listed too:
println(File.list("config"))Ok{ value: [".env", "app.json"] }The order compares names as strings do, code point by code point: uppercase letters before lowercase, 10 before 9, and Ä after every ASCII letter:
println(File.list("mixed"))Ok{ value: ["10.txt", "9.txt", "Zeta", "apple", "Äpfel"] }A symbolic link is an entry of its own, listed by its name:
try File.symlink("target.txt", "links/latest")
println(File.list("links"))Ok{ value: ["latest", "target.txt"] }Applications
Keep the names with one extension:
func markdownFiles(dir: string): Result[Array[string], Error] {
let names = try File.list(dir)
Result.ok(names.filter({ x.endsWith(".md") }))
}
println(markdownFiles("posts"))Ok{ value: ["hello.md", "world.md"] }Report the size of each file in a directory, skipping subdirectories:
func fileSizes(dir: string): Result[Map[string, int], Error] {
let sizes: Map[string, int] = Map{}
for name in try File.list(dir) {
let info = try File.stat(Path.join([dir, name]))
if !info.isDir {
sizes[name] = info.size
}
}
Result.ok(sizes)
}
println(fileSizes("notes"))Ok{ value: Map{ "ideas.md": 8, "todo.txt": 9 } }Refuse to work in a directory that is not empty:
func checkEmpty(dir: string): Result[void, Error] {
let names = try File.list(dir)
if !names.isEmpty() {
return Result.fail("${dir} is not empty: ${names | String.join(", ")}")
}
Result.ok(void)
}
println(checkEmpty("build"))Err{ error: Error{ message: "build is not empty: old.js" } }Properties and relations
The names come sorted, so sorting them again changes nothing:
let names = try File.list("mixed")
assert(names == names.sort())The names are those of the entries File.readDir returns, in the same order:
assert((try File.list("notes")) == (try File.readDir("notes")).map({ x.name }))File.list gives what File.glob gives for * with hidden names included:
assert(File.list("config") == File.glob("*", dir: "config", hidden: true))In a directory that holds only files, File.list and File.walk agree. Once there is a subdirectory, File.list names it and File.walk lists the files inside it instead:
println(File.list("notes"))
println(File.walk("notes"))Ok{ value: ["archive", "ideas.md", "todo.txt"] }
Ok{ value: ["archive/2025.md", "ideas.md", "todo.txt"] }Possible issues
The names are relative to dir, not to the working directory. Passing one straight to File.read looks for it in the wrong place:
let names = try File.list("inbox")
println(File.read(names[0]))
println(File.read(Path.join(["inbox", names[0]])))Err{ error: Error{ message: "open todo.txt: no such file or directory" } }
Ok{ value: "buy milk\n" }A directory that does not exist, and a file in place of a directory, are errors:
println(File.list("missing"))
println(File.list("notes/todo.txt"))Err{ error: Error{ message: "open missing: no such file or directory" } }
Err{ error: Error{ message: "open notes/todo.txt: not a directory" } }See also
File.readDir, File.walk, File.glob, File.stat, File.exists, File.mkdir, Path.join
