Path.base
func Path.base(p: string): stringPath.base returns the last element of p after dropping trailing separators, the separator for a path made only of separators, or "." when p is empty.
Details
Path.baselooks only at the text. It does not touch the file system and does not cleanpfirst, soPath.base("a/..")is"..".- Trailing separators are dropped first, so
Path.base("a/b/")is"b"; a path made only of separators gives"/".
Basic examples
The file name at the end of a path:
Path.base("src/main.ant") // "main.ant"
Path.base("/usr/local/bin") // "bin"A name with no directory is its own base:
Path.base("README.md") // "README.md"Scope
The empty path gives ".":
Path.base("") // "."., .., and hidden names are elements like any other:
Path.base(".") // "."
Path.base("../..") // ".."
Path.base("home/ann/.env") // ".env"Unicode names come back whole:
Path.base("docs/résumé.pdf") // "résumé.pdf"
Path.base("фото/кот.jpg") // "кот.jpg"Path.base works as a function value:
let paths = ["src/a.ant", "src/lib/b.ant", "test/c_test.ant"]
paths.map(Path.base) // ["a.ant", "b.ant", "c_test.ant"]It is in the Path namespace, not String, so a string has no .base():
let p = "src/a.ant"
let name = p.base()
// error: string has no field or function baseApplications
The name without its extension, with Path.ext:
let path = "reports/2024/summary.csv"
let name = Path.base(path)
name.trimSuffix(Path.ext(name)) // "summary"Move a file into another directory under the same name:
let from = "downloads/invoice-42.pdf"
Path.join(["archive", Path.base(from)]) // "archive/invoice-42.pdf"Find file names that occur in more than one directory:
let files = ["a/config.json", "b/data.csv", "c/config.json", "d/notes.txt"]
let byName = files | Array.groupBy(Path.base)
byName
| Map.entries()
| Array.filter({ x.value.length() > 1 })
| Array.map({ x.key }) // ["config.json"]List the files a directory holds by name only:
try File.mkdir("logs")
try File.write("logs/app.log", "")
try File.write("logs/db.log", "")
let names = (try File.glob("logs/*.log")) | Array.map(Path.base) | String.join(", ")
println(names)app.log, db.logProperties and relations
For a cleaned path with no trailing separator, Path.join of Path.dir and Path.base gives the path back:
let p = "src/lib/util.ant"
assert(Path.join([Path.dir(p), Path.base(p)]) == p)The base of a base is the same name:
let p = "a/b/c.txt"
assert(Path.base(Path.base(p)) == Path.base(p))Possible issues
Path.base does not resolve ... Clean the path first with Path.clean to get the name the path leads to:
Path.base("photos/2024/..") // ".."
Path.base(Path.clean("photos/2024/..")) // "photos"The empty path gives ".", not "", so a missing name looks like the current directory. Check for the empty string before taking the name:
func fileName(input: string): string | null {
if input.isEmpty() { return null }
Path.base(input)
}
fileName("") // null
fileName("a/b.txt") // "b.txt"The separator is the host's. On Unix a backslash is part of the name, so a Windows-style path comes back whole. On Windows both / and \ separate:
Path.base("dir\\file.txt") // "dir\\file.txt"See also
Path.dir · Path.ext · Path.join · Path.clean · Path.abs · File.glob
