Path.dir
func Path.dir(p: string): stringPath.dir returns p up to its last separator, cleaned, or "." when there is none.
Details
- Works on the text alone: the path does not have to exist, and nothing on disk is read.
- Separators are the host's:
/on Unix,\and drive letters on Windows. Path.diris the path up to its last separator, cleaned, soPath.dir("a/b/")is"a/b"andPath.dir("/")is"/". It rebuildspwith Path.base only whenpdoes not end in a separator.
Basic examples
The directory part of a file path:
Path.dir("a/b.ant") // "a"
Path.dir("b.ant") // "."An absolute path keeps its root:
Path.dir("/home/ann/notes.txt") // "/home/ann"Scope
A path with one element, and the empty path, have no directory part, so the result is ".":
Path.dir("notes.txt") // "."
Path.dir("") // "."An element right under the root gives the root:
Path.dir("/etc") // "/"Paths that climb with .. keep it:
Path.dir("../shared/lib.ant") // "../shared"Elements of any script come back as they are:
Path.dir("文書/日本語.txt") // "文書"Call it in a pipe, with the argument named, or inside a lambda:
"src/main.ant" | Path.dir() // "src"
Path.dir(p: "src/main.ant") // "src"
["a/x.ant", "b/y.ant"].map({ Path.dir(x) }) // ["a", "b"]Applications
Name a file next to another one, in the same directory:
func sibling(path: string, name: string): string {
Path.join([Path.dir(path), name])
}
sibling("config/app.toml", "app.lock") // "config/app.lock"
sibling("app.toml", "app.lock") // "app.lock"Group files by the directory that holds them:
let files = ["src/main.ant", "src/util.ant", "tests/main_test.ant", "README.md"]
let byDir = files | Array.groupBy({ Path.dir(x) })
println(byDir)Map{ "src": ["src/main.ant", "src/util.ant"], "tests": ["tests/main_test.ant"], ".": ["README.md"] }List every directory above a relative path, nearest first:
func parents(path: string): Array[string] {
let dirs: Array[string] = []
var dir = Path.dir(path)
while dir != "." {
dirs.push(dir)
dir = Path.dir(dir)
}
dirs
}
println(parents("a/b/c/d.ant"))["a/b/c", "a/b", "a"]Create the directory a file goes in before writing it:
func save(path: string, text: string): Result[void, Error] {
try File.mkdir(Path.dir(path)).wrapErr("save ${path}")
File.write(path, text).wrapErr("save ${path}")
}
save("out/reports/today.txt", "done").unwrap()
println(File.read("out/reports/today.txt").unwrap())doneProperties and relations
For a path that does not end in a separator, Path.dir and Path.base split it into two parts that Path.join joins back into the cleaned path:
for p in ["a/b.ant", "b.ant", "/home/ann/notes.txt", "../shared/lib.ant"] {
assert(Path.join([Path.dir(p), Path.base(p)]) == Path.clean(p))
}Possible issues
A trailing separator makes the last element empty, so Path.dir returns the directory itself, not its parent:
Path.dir("logs/2026/") // "logs/2026"
Path.dir("logs/2026") // "logs"Remove the separator first with String.trimSuffix when the path may end in one:
"logs/2026/".trimSuffix("/") | Path.dir() // "logs"Path.dir takes a string, which belongs to the String namespace, so a dot call does not find it. Call it by its namespace or in a pipe:
let path = "src/main.ant"
println(path.dir())
// error: string has no field or function dirSee also
Path.base · Path.ext · Path.join · Path.clean · Path.abs · File.mkdir
