Skip to content
Kavachकवच

Files: Folders

● Intermediate3 min

fs.create_dir makes a folder and its parents; fs.list gives the names in a folder, sorted.

use std.fs

fn main() ! Error {
    try fs.create_dir("photos/2026/june")
    try fs.write_text("photos/2026/june/beach.txt", "sea")
    try fs.write_text("photos/2026/june/hills.txt", "green")
    print(try fs.list("photos/2026/june"))
    print(fs.exists("photos/2026"))
    try fs.remove("photos/2026/june/beach.txt")
    print(try fs.list("photos/2026/june"))
}

The program ran and printed:

kavach run folders.kav

["beach.txt", "hills.txt"]true["hills.txt"]
folders.kav: Kavach code, then what running it prints

fs.remove deletes a file or an empty folder, and fs.exists says whether something is there. Paths are relative to the folder the program runs in.

Copy, rename and remove a whole folder

use std.fs

fn main() ! FsError uses Fs, Console {
    try fs.create_dir("reports/2026")
    try fs.write_text("reports/2026/may.txt", "sales 120")
    try fs.copy("reports/2026/may.txt", "reports/2026/june.txt")
    try fs.rename("reports/2026/may.txt", "reports/2026/may-final.txt")
    print(try fs.list("reports/2026"))

    try fs.remove_all("reports")
    print("still there: {fs.exists("reports")}")
}

The program ran and printed:

kavach run move.kav

["june.txt", "may-final.txt"]still there: false
move.kav: Kavach code, then what running it prints
  • fs.copy copies a file; fs.rename moves or renames a file or a folder.
  • fs.remove_all deletes a folder with everything in it.
  • Under uses Fs("folder"), both paths of copy and rename must be inside that folder, so a file can't be carried out of it.
  • fs.symlink(target, link) makes a symbolic link. Under uses Fs("folder") both must be inside the folder, so a link can't lead out of it. Windows allows links only in Developer Mode or with administrator rights; otherwise it fails with Denied.