File.readDir
func File.readDir(dir: string): Result[Array[File.Entry], Error]File.readDir returns the entries directly in dir, in the order of File.list, each with its name and its kind. The kind comes with the listing, so telling directories from files costs no File.stat per entry. It follows a link at dir. An entry that is a symbolic link is reported as the link, whatever it points to.
Details
- Each entry is a File.Entry with the fields
name,isDir, andisSymlink. - The entries come in lexical order, the order of File.list.
- Names that begin with a dot and symbolic links are entries like any other, as in File.list.
- The names are bare, as those of File.list are: to read or stat an entry, join its name to
dirwithPath.join. - The kind is the one the directory holds. A symbolic link has
isSymlinktrue andisDirfalse, also when it points to a directory; File.stat of the path tells what it points to. - An entry has no size, time, or mode. Call File.stat for those.
- An empty directory gives
Okwith an empty array. A missingdir, or adirthat is a file, isErr.
Basic examples
List the entries of a directory with their kinds:
for entry in try File.readDir("notes") {
println(entry)
}File.Entry{ name: "archive", isDir: true, isSymlink: false }
File.Entry{ name: "todo.txt", isDir: false, isSymlink: false }Mark the directories with a slash, as ls -p does:
for { name, isDir } in try File.readDir("notes") {
println(if isDir { name + "/" } else { name })
}archive/
todo.txtScope
The entries come in the order of File.list, and with the same names:
let entries = try File.readDir("mixed")
println(entries.map({ x.name }))
println(entries.map({ x.name }) == try File.list("mixed"))["10.txt", "9.txt", "Zeta", "apple"]
trueA symbolic link is reported as a link, whether it points to a file, to a directory, or to nothing:
try File.symlink("target.txt", "links/to-file")
try File.symlink("sub", "links/to-dir")
try File.symlink("nowhere", "links/dangling")
for entry in try File.readDir("links") {
println(entry)
}File.Entry{ name: "dangling", isDir: false, isSymlink: true }
File.Entry{ name: "sub", isDir: true, isSymlink: false }
File.Entry{ name: "target.txt", isDir: false, isSymlink: false }
File.Entry{ name: "to-dir", isDir: false, isSymlink: true }
File.Entry{ name: "to-file", isDir: false, isSymlink: true }A link at dir itself is followed:
try File.symlink("sub", "shortcuts/to-dir")
println(File.readDir("shortcuts/to-dir"))Ok{ value: [File.Entry{ name: "inner.txt", isDir: false, isSymlink: false }] }An empty directory gives an empty array, and a path that is not a directory is Err:
try File.mkdir("empty")
try File.write("notes.txt", "")
println(File.readDir("empty"))
println(File.readDir("notes.txt") is Err)
println(File.readDir("missing") is Err)Ok{ value: [] }
true
trueApplications
Split a listing into directories and files:
let entries = try File.readDir("site")
println(entries.filter({ x.isDir }).map({ x.name }))
println(entries.filter({ !x.isDir }).map({ x.name }))["css", "img"]
["index.html"]Count the files below a directory, entering directories and not links:
func countFiles(dir: string): Result[int, Error] {
var n = 0
for entry in try File.readDir(dir) {
if entry.isDir {
n += try countFiles(Path.join([dir, entry.name]))
} else {
n += 1
}
}
Result.ok(n)
}
println(countFiles("web"))Ok{ value: 3 }Treat a link to a directory as a directory, asking File.stat only about the links:
func isDirectory(dir: string, entry: File.Entry): bool {
if !entry.isSymlink { return entry.isDir }
let info = File.stat(Path.join([dir, entry.name]))
info is Ok && info.value.isDir
}
try File.symlink("guide", "docs/latest")
for entry in try File.readDir("docs") {
println("${entry.name}: ${isDirectory("docs", entry)}")
}guide: true
latest: true
readme.md: falseProperties and relations
For an entry that is not a link, isDir is what File.stat reports for its path:
for entry in try File.readDir("notes") {
let info = try File.stat(Path.join(["notes", entry.name]))
println(entry.isDir == info.isDir)
}true
trueFor a link, the entry agrees with File.stat when it is told not to follow:
try File.symlink("sub", "refs/to-dir")
let entry = (try File.readDir("refs"))[1]
let info = try File.stat("refs/to-dir", follow: false)
println(entry.name)
println(entry.isSymlink == info.isSymlink)
println(entry.isDir == info.isDir)to-dir
true
trueFile.readDir reports the directories in dir, empty ones too, and does not descend. File.walk lists only the files below:
try File.mkdir("project/build")
println((try File.readDir("project")).map({ x.name }))
println(try File.walk("project"))["build", "readme.md", "src"]
["readme.md", "src/main.ant"]Possible issues
isDir is false for a link to a directory, so code that descends on isDir alone does not enter it. That is usually what a walk wants, since a link can point back up the tree:
try File.symlink("data", "tree/alias")
for entry in try File.readDir("tree") {
println("${entry.name}: isDir ${entry.isDir}")
}alias: isDir false
data: isDir trueThe names are not paths. Join them to the directory before using them:
let entry = (try File.readDir("inbox"))[0]
println(File.read(entry.name) is Err)
println(File.read(Path.join(["inbox", entry.name])))true
Ok{ value: "buy milk\n" }See also
- File.Entry: what each entry holds
- File.list: the names alone
- File.stat: the size, times, and mode of one path, and what a link points to
- File.walk: every file below a directory
- File.glob: the paths that match a pattern
