File.stat
func File.stat(path: string, follow: bool = true): Result[File.Info, Error]File.stat returns the name, size, kind, modification time, mode and owner of the file at path. It follows a symbolic link and describes its target, unless follow is false, when it describes the link itself.
Details
- A dangling link is
Errunlessfollowisfalse, andisSymlinkis true only withfollow: false. modifiedis the time of the last write, as precise as the file system keeps it: to the nanosecond on ext4 and APFS, to the second on HFS+, to 2 seconds on FAT.modeholds the permissions, not the kind of file:isDirandisSymlinksay that.uidandgidare the numeric ids of the owning user and group,nullon Windows and WebAssembly; File.chown changes them.File.statisErrwhen nothing is atpath.
Basic examples
Read the size of a file:
try File.write("notes.txt", "buy milk")
let info = try File.stat("notes.txt")
println("${info.name}: ${info.size} bytes")notes.txt: 8 bytesTell a directory from a file:
try File.mkdir("logs")
try File.write("logs/today.log", "started")
println(File.stat("logs").map({ x.isDir }))
println(File.stat("logs/today.log").map({ x.isDir }))Ok{ value: true }
Ok{ value: false }A missing path is an Err:
println(File.stat("missing.txt"))Err{ error: Error{ message: "stat missing.txt: no such file or directory" } }Scope
Read the name and size of a nested file:
try File.mkdir("src/lib")
try File.write("src/lib/geo.ant", "")
let info = try File.stat("src/lib/geo.ant")
println(info.name)
println(info.size)geo.ant
0A Unicode name works:
try File.write("café.txt", "☕")
println((try File.stat("café.txt")).name)café.txtmodified is a Time.Instant. Set it with File.touch and read it back:
let release = try Time.parseInstant("2026-03-01T12:00:00Z")
try File.write("CHANGES.md", "# 1.0")
try File.touch("CHANGES.md", time: release)
println((try File.stat("CHANGES.md")).modified)2026-03-01T12:00:00Zmode is a number; print it in octal with String.fromInt:
try File.write("deploy.sh", "echo deploying\n")
try File.chmod("deploy.sh", 0o755)
let info = try File.stat("deploy.sh")
println(String.fromInt(info.mode, base: 8))755uid is the id of the user that owns the file, the same as Os.uid for a file this program just wrote:
try File.write("mine.txt", "x")
let info = try File.stat("mine.txt")
println(info.uid == Os.uid())trueA path that runs through a file is an Err, as is an empty path:
try File.write("plain.txt", "text")
println(File.stat("plain.txt/inner"))
println(File.stat(""))Err{ error: Error{ message: "stat plain.txt/inner: not a directory" } }
Err{ error: Error{ message: "empty path" } }Named arguments
follow
By default File.stat follows a link and describes its target. With follow: false it describes the link itself:
try File.write("v2.txt", "second")
try File.chmod("v2.txt", 0o600)
try File.symlink("v2.txt", "current")
let target = try File.stat("current")
let link = try File.stat("current", follow: false)
println("${target.name} ${target.isSymlink} ${String.fromInt(target.mode, base: 8)}")
println("${link.name} ${link.isSymlink}")current false 600
current trueA link whose target is missing is an Err when followed, and Ok with follow: false:
try File.symlink("gone.txt", "broken")
println(File.stat("broken"))
println(File.stat("broken", follow: false).map({ x.isSymlink }))Err{ error: Error{ message: "stat broken: no such file or directory" } }
Ok{ value: true }A link to a directory is a directory when followed, and a link, not a directory, with follow: false:
try File.mkdir("releases/v1.5.0")
try File.symlink("releases/v1.5.0", "live")
let followed = try File.stat("live")
let link = try File.stat("live", follow: false)
println("${followed.isDir} ${followed.isSymlink}")
println("${link.isDir} ${link.isSymlink}")true false
false trueApplications
Add up the sizes of the files below a directory:
try File.mkdir("site/css")
try File.write("site/index.html", "<h1>Home</h1>")
try File.write("site/css/main.css", "body { margin: 0 }")
var total = 0
for path in try File.walk("site") {
total += (try File.stat(Path.join(["site", path]))).size
}
println("${total} bytes")31 bytesRebuild an output only when its source changed after it, as make does:
func stale(source: string, output: string): Result[bool, Error] {
if !File.exists(output) { return Result.ok(true) }
let src = try File.stat(source)
let out = try File.stat(output)
Result.ok(Time.compare(src.modified, out.modified) > 0)
}
try File.write("app.ant", "println(1)")
try File.write("app.js", "console.log(1)")
try File.touch("app.ant", time: try Time.parseInstant("2026-05-02T09:00:00Z"))
try File.touch("app.js", time: try Time.parseInstant("2026-05-01T09:00:00Z"))
println(stale("app.ant", "app.js"))
println(stale("app.ant", "app.wasm"))
try File.touch("app.js", time: try Time.parseInstant("2026-05-03T09:00:00Z"))
println(stale("app.ant", "app.js"))Ok{ value: true }
Ok{ value: true }
Ok{ value: false }Check that a script is executable by its owner before running it:
func checkExecutable(path: string): Result[void, Error] {
let info = try (File.stat(path) | Result.wrapErr("check ${path}"))
if Bits.and(info.mode, 0o100) == 0 {
return Result.fail("${path} is not executable; run chmod +x ${path}")
}
Result.ok(void)
}
try File.write("build.sh", "echo building\n")
try File.chmod("build.sh", 0o644)
println(checkExecutable("build.sh"))
println(checkExecutable("test.sh"))Err{ error: Error{ message: "build.sh is not executable; run chmod +x build.sh" } }
Err{ error: Error{ message: "check test.sh: stat test.sh: no such file or directory" } }Properties and relations
size matches the length of the file's bytes, not of its text:
try File.write("menu.txt", "café ☕\n")
let info = try File.stat("menu.txt")
assert(info.size == (try File.readBytes("menu.txt")).length())
assert(info.size != (try File.read("menu.txt")).length())File.exists follows links as File.stat does, so a dangling link is false for one and Err for the other:
try File.write("a.txt", "")
try File.symlink("a.txt", "good")
try File.symlink("gone.txt", "bad")
for path in ["a.txt", "good", "bad", "nothing"] {
assert(File.exists(path) == File.stat(path).isOk())
}Following a link gives the target's size, mode and time:
try File.write("data.csv", "a,b\n1,2\n")
try File.symlink("data.csv", "latest.csv")
let file = try File.stat("data.csv")
let viaLink = try File.stat("latest.csv")
assert(viaLink.size == file.size)
assert(viaLink.mode == file.mode)
assert(viaLink.modified == file.modified)For each entry of a directory, File.readDir reports the isDir and isSymlink that File.stat reports with follow: false, in one call:
try File.mkdir("project/src")
try File.write("project/README.md", "# Project")
try File.symlink("README.md", "project/readme")
for entry in try File.readDir("project") {
let info = try File.stat(Path.join(["project", entry.name]), follow: false)
assert(entry.isDir == info.isDir && entry.isSymlink == info.isSymlink)
}Use File.readDir when only the kind is wanted, and File.stat when the size, modified time, mode or owner is.
Possible issues
Without follow: false, isSymlink is false, so a check for links that leaves it out never finds one:
try File.write("todo.txt", "")
try File.symlink("todo.txt", "today")
println((try File.stat("today")).isSymlink)
println((try File.stat("today", follow: false)).isSymlink)false
truesize and the text's length differ for non-ASCII text. For the number of characters, read the text and take its length:
try File.write("greeting.txt", "héllo")
println((try File.stat("greeting.txt")).size)
println((try File.read("greeting.txt")).length())6
5modified is a Time.Instant, which < and > do not order; src.modified > out.modified is a compile error. Use Time.compare:
try File.write("older.txt", "")
try File.write("newer.txt", "")
try File.touch("older.txt", time: try Time.parseInstant("2026-01-01T00:00:00Z"))
try File.touch("newer.txt", time: try Time.parseInstant("2026-02-01T00:00:00Z"))
let older = try File.stat("older.txt")
let newer = try File.stat("newer.txt")
println(Time.compare(older.modified, newer.modified))-1See also
File.exists · File.Info · File.touch · File.chmod · File.chown · File.readLink · File.realPath · File.list · File.readDir · File.Entry · File.walk
