Home About Documentation Templates Examples Showcase GitHub
Theme

Scripting · Filesystem

Filesystem operations.

Script land includes a readable filesystem API. Nift 4.4 generalizes it with normal standalone filesystem authority, variadic path operations and deterministic wildcards.

print(pwd())

if(!exists("work")) {
    make_dir("work")
}

touch("work/input.txt")
copy("work/input.txt", "work/copy.txt")
move("work/copy.txt", "work/final.txt")
entries := ls("work")
print(entries.prettify())
cat("work/final.txt")
text := open("work/final.txt")
remove("work/final.txt")

Wildcards and multiple paths (v4.4)

objects := ls("build/**/*.o")
remove("*.o", "*.tmp")
copy(["assets/logo.svg", "assets/icon.svg"], "public/assets")
move("generated/*.json", "archive")

* and ? match within one path component; a complete ** component recursively matches directory levels. Results are deterministic and hidden entries are not matched unless the pattern explicitly starts that component with .. Arrays and ...spread compose with the same APIs.

OperationPurpose
pwd()Return the current script working directory.
cd(path)Change directory under standalone nift script.f/nift hosts.
exists(path)Test whether a path exists (symlinks followed).
is_file(path)Test whether a path is an existing regular file.
is_dir(path)Test whether a path is an existing directory.
stat(path)Return metadata for a path: {exists:false} when absent, otherwise {exists:true, type:"file", size:N} or {exists:true, type:"directory"} (no size for directories). Permission/metadata failures raise a controlled recoverable error rather than a wrong answer.
ls() / ls(path)Return a deterministically filename-sorted array of directory entry names.
cat(path)Write the file contents exactly to stdout, without adding a trailing newline; returns null.
make_dir(path)Create the directory path, including missing parents.
touch(path)Create the file if needed or open it for append without replacing its contents.
copy(from, to)Copy a file, replacing an existing destination file.
move(from, to)Move/rename a path using the host filesystem operation.
remove(path)Remove a file. Directory removal is deliberately not recursive.
open(path)Load the entire file as a string.

Build-hosted scripts remain confined by Nift's project/path rules. Standalone nift script.f and nift use normal OS-user filesystem authority: relative/parent, absolute and ~ home paths are available subject to OS permissions. Build-hosted paths retain Nift's dependency/output safety rules. open(path) is data access; it is intentionally distinct from template @input, script import(path) and source injection.

Backend filesystem failures on an otherwise valid operation are recoverable and can be caught (io.open_failed, io.read_failed, io.create_failed, io.write_failed, io.copy_failed, io.move_failed, io.remove_failed, io.directory_read_failed, io.change_directory_failed, io.atomic_replace_failed). Invalid argument/type, authority/path escape, wrong target type and lifecycle misuse stay fatal.