File.remove
func File.remove(path: string): Result[void, Error]File.remove removes the file or empty directory at path. It is Err when nothing is there and on a directory that has entries; File.removeAll removes a whole tree.
Details
- Nothing at
pathisErr, unlike File.removeAll, which isOkthen. - At a symbolic link it removes the link, never what the link points at.
Basic examples
Remove a file:
println(File.remove("draft.txt"))
println(File.exists("draft.txt"))Ok{ value: void }
falseRemove a directory once it is empty:
try File.mkdir("empty")
println(File.remove("empty"))Ok{ value: void }Scope
A directory with entries is Err, and so is a path where nothing is:
println(File.remove("build"))
println(File.remove("missing.txt"))Err{ error: Error{ message: "remove build: directory not empty" } }
Err{ error: Error{ message: "remove missing.txt: no such file or directory" } }A symbolic link is removed itself, and its target stays. A dangling link is removed the same way:
try File.symlink("notes.txt", "current")
try File.symlink("gone.txt", "broken")
println(File.remove("current"))
println(File.remove("broken"))
println(File.exists("notes.txt"))Ok{ value: void }
Ok{ value: void }
trueApplications
Remove a temporary file however the function returns. After the rename the deferred remove is Err, and a deferred block's value is discarded:
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)
}
println(save("out.txt", "hi"))
println(try File.glob(".save-*", hidden: true))Ok{ value: void }
[]Delete the log files of a directory and keep everything else:
for path in try File.glob("*.log", dir: "logs") {
try File.remove(Path.join(["logs", path]))
}
println(try File.list("logs"))["README"]Remove a file that may already be gone, and fail only when it is still there afterwards:
func discard(path: string): Result[void, Error] {
let removed = File.remove(path)
if removed.isOk() || !File.exists(path) { return Result.ok(void) }
removed
}
println(discard("cache.json"))
println(discard("cache.json"))Ok{ value: void }
Ok{ value: void }Properties and relations
After an Ok, nothing is at the path, and a second call is Err:
try File.write("draft.txt", "first line")
assert(File.remove("draft.txt").isOk())
assert(!File.exists("draft.txt"))
assert(!File.remove("draft.txt").isOk())On a file or an empty directory File.remove and File.removeAll do the same; on a missing path only File.removeAll is Ok:
try File.mkdir("a")
try File.mkdir("b")
assert(File.remove("a").isOk() == File.removeAll("b").isOk())
assert(!File.remove("a").isOk())
assert(File.removeAll("b").isOk())Possible issues
File.remove does not empty a directory first. Use File.removeAll for a tree:
println(File.remove("out"))
println(File.removeAll("out"))Err{ error: Error{ message: "remove out: directory not empty" } }
Ok{ value: void }A call on its own line drops the Result, so a failed removal goes unnoticed. Use try to stop at the error; at the top of a script it prints the message and exits with status 1:
_ = File.remove("report.txt")
println("removed, or so it seems")
try File.remove("report.txt")
println("not reached")removed, or so it seemsSee also
File.removeAll · File.exists · File.rename · File.tempFile · File.tempDir · File.mkdir · File.symlink · File.glob
