File.mkdir
func File.mkdir(path: string): Result[void, Error]File.mkdir creates the directory at path, with any parents it lacks, each made as the path names it, so x/../y makes x too. It is Ok when the directory already exists, a link to one included, and Err at a dangling link.
Details
- A new directory gets mode
0o777less the process's umask, so0o755under the usual umask022. Use File.chmod afterwards for an exact mode. - A directory that already exists is left as it is, mode included.
- It is
Errwhen a file that is not a directory is atpathor at one of its parents.
Basic examples
Create a directory and the parents it needs in one call:
println(File.mkdir("build/assets/icons"))
let info = try File.stat("build/assets")
println(info.isDir)Ok{ value: void }
trueCall it again and it is still Ok:
try File.mkdir("cache")
println(File.mkdir("cache"))Ok{ value: void }Scope
The current directory, ".", always exists, so File.mkdir(".") is Ok:
println(File.mkdir("."))Ok{ value: void }Applications
Write a file at a nested path, making its directory first. Path.dir of a bare file name is ".", so the helper works for a file in the current directory too:
func save(path: string, text: string): Result[void, Error] {
try File.mkdir(Path.dir(path))
File.write(path, text)
}
try save("reports/2026/11/summary.txt", "all green")
try save("notes.txt", "top level")
println(try File.walk("reports"))
println(try File.read("notes.txt"))["2026/11/summary.txt"]
top levelKeep a script's files under a directory of its own name in the user's cache directory:
let dir = Path.join([try Os.cacheDir(), "thumbnailer"])
try File.mkdir(dir)
try File.write(Path.join([dir, "index.json"]), "{}")
println("cache in ${dir}")Tighten an ordinary directory so that only its owner can enter it. Between the two calls it has the umask mode, so for a directory that must be private from the moment it exists, use File.tempDir, which is 0o700 from the start:
let drafts = "drafts"
try File.mkdir(drafts)
try File.chmod(drafts, 0o700)
let info = try File.stat(drafts)
println(info.mode | String.fromInt(base: 8))700Properties and relations
After an Ok, the path is a directory, and File.exists agrees:
let dir = "a/b/c"
try File.mkdir(dir)
let info = try File.stat(dir)
assert(info.isDir)
assert(File.exists(dir))Possible issues
File.mkdir does not reset the mode of a directory that is already there. A directory left at 0o755 stays so after another call:
try File.mkdir("shared")
try File.chmod("shared", 0o755)
try File.mkdir("shared")
let info = try File.stat("shared")
println(info.mode | String.fromInt(base: 8))755For an exact mode, call File.chmod after every File.mkdir:
try File.mkdir("shared")
try File.chmod("shared", 0o755)
try File.mkdir("shared")
try File.chmod("shared", 0o700)
let info = try File.stat("shared")
println(info.mode | String.fromInt(base: 8))700A file in the way, at the path or at one of its parents, makes it Err:
println(File.mkdir("notes"))
println(File.mkdir("notes/2026"))Err{ error: Error{ message: "mkdir notes: not a directory" } }
Err{ error: Error{ message: "mkdir notes: not a directory" } }File.write does not create missing directories. Call File.mkdir first:
println(File.write("logs/today.log", "started"))
try File.mkdir("logs")
println(File.write("logs/today.log", "started"))Err{ error: Error{ message: "open logs/today.log: no such file or directory" } }
Ok{ value: void }See also
File.tempDir · File.chmod · File.remove · File.removeAll · File.list · File.stat · File.exists · Path.dir
