File.rename
func File.rename(from: string, to: string): Result[void, Error]File.rename renames or moves the file or directory at from to the path to, replacing a file at to. It is Err when a directory is at to, since there is no "into" form, and across file systems; it is Ok and nothing changes when from and to are the same file.
Details
File.renamereplaces a file attowithout asking. It has noreplace:parameter.- On Unix the replacement is atomic: a reader of
tosees the old file or the new one, never a mix. Elsewhere atomicity is not promised. - Atomic is not durable: the rename can reach the disk before the file's data does. Sync the new file with File.sync before renaming it into place, as Replace a file without a partial write shows.
- At a symbolic link,
File.renamemoves the link itself, not what it points at.
Basic examples
Rename a file:
try File.write("draft.txt", "hello")
println(File.rename("draft.txt", "final.txt"))
println(File.exists("draft.txt"))
println(try File.read("final.txt"))Ok{ value: void }
false
helloA source that does not exist is Err:
println(File.rename("missing.txt", "other.txt"))Err{ error: Error{ message: "rename missing.txt other.txt: no such file or directory" } }Scope
Directories
A directory moves with everything in it:
try File.mkdir("logs/2026")
try File.write("logs/2026/app.log", "started")
try File.rename("logs", "archive")
println(File.exists("logs"))
println(try File.walk("archive"))false
["2026/app.log"]Moving to another directory
A to in another directory moves the file there.
try File.mkdir("bin")
try File.write("deploy.sh", "echo deploying")
try File.rename("deploy.sh", "bin/deploy.sh")
println(try File.read("bin/deploy.sh"))echo deployingReplacing a file
A file already at to is replaced:
try File.write("config.new", "port = 8080")
try File.write("config", "port = 80")
try File.rename("config.new", "config")
println(try File.read("config"))
println(File.exists("config.new"))port = 8080
falseSymbolic links
The link moves and keeps its target, which stays where it was:
try File.write("v2.txt", "version 2")
try File.symlink("v2.txt", "next")
try File.rename("next", "current")
println(try File.readLink("current"))
println(try File.read("v2.txt"))v2.txt
version 2Applications
Save a file so that no reader ever sees it half-written: write a temporary file beside it, then rename it into place. The deferred File.remove cleans up when a step fails, and is an Err that nobody reads after the rename succeeds. This keeps readers from seeing a partial file but does not survive a crash. For that, write through File.create and call File.sync before the rename, as Replace a file without a partial write shows.
func save(path: string, text: string): Result[void, Error] {
let tmp = try File.tempFile(".save-*", dir: Path.dir(path))
defer { File.remove(tmp) }
try File.write(tmp, text)
File.rename(tmp, path)
}
try File.write("settings.json", "{\"theme\": \"light\"}")
try save("settings.json", "{\"theme\": \"dark\"}")
println(try File.read("settings.json"))
println(try File.glob(".save-*", hidden: true)){"theme": "dark"}
[]Change the extension of every .txt file in a directory:
let dir = "notes"
for name in try File.list(dir) {
if Path.ext(name) != ".txt" { continue }
let from = Path.join([dir, name])
try File.rename(from, from.trimSuffix(".txt") + ".md")
}
println(try File.list(dir))["a.md", "b.md", "c.md"]Move a file into a directory, joining the directory and the file's name, and add context to a failure with Result.wrapErr:
func moveInto(path: string, dir: string): Result[void, Error] {
try File.mkdir(dir)
File.rename(path, Path.join([dir, Path.base(path)]))
| Result.wrapErr("archive ${path}")
}
try File.write("report.csv", "id,total")
try moveInto("report.csv", "done/2026")
println(try File.walk("done"))
println(moveInto("report.csv", "done/2026"))["2026/report.csv"]
Err{ error: Error{ message: "archive report.csv: rename report.csv done/2026/report.csv: no such file or directory" } }Properties and relations
After an Ok, nothing is at from, and to holds what from held:
let text = "contents"
try File.write("old.txt", text)
try File.rename("old.txt", "new.txt")
assert(!File.exists("old.txt"))
assert(try File.read("new.txt") == text)Possible issues
File.rename replaces a file at to without warning. Checking with File.exists first is not atomic: another program can create to between the check and the rename.
try File.write("notes.txt", "keep me")
try File.write("draft.txt", "scratch")
try File.rename("draft.txt", "notes.txt")
println(try File.read("notes.txt"))scratchto is the new path of the file, not a directory to move it into. To move a file into a directory, join the directory and the name with Path.join:
try File.mkdir("box")
try File.write("item.txt", "x")
try File.rename("item.txt", Path.join(["box", "item.txt"]))
println(try File.walk("box"))["item.txt"]The directory of to must exist. Create it with File.mkdir first:
try File.write("photo.jpg", "jpeg")
println(File.rename("photo.jpg", "albums/2026/photo.jpg"))
try File.mkdir("albums/2026")
println(File.rename("photo.jpg", "albums/2026/photo.jpg"))Err{ error: Error{ message: "rename photo.jpg albums/2026/photo.jpg: no such file or directory" } }
Ok{ value: void }See also
File.copy · File.copyAll · File.remove · File.tempFile · File.sync · File.mkdir · File.exists · Path.join · Path.base
