system.fs

File system operations: reading, writing, and managing files and directories.

import system.fs as fs

Reading Files

readFile(path)

Read entire file as string.

let content = fs.readFile("config.txt")
io.println(content)

readLines(path)

Read file as array of lines.

let lines = fs.readLines("data.txt")
for (let i = 0; i < len(lines); i = i + 1) {
io.println("Line " + (i+1) + ": " + lines[i])
}

readBytes(path)

Read file as byte array.

let bytes = fs.readBytes("image.png")
io.println("Size: " + len(bytes) + " bytes")

Writing Files

writeFile(path, content)

Write string to file (overwrites existing).

fs.writeFile("output.txt", "Hello, World!")

// Multi-line content
let data = "Line 1\nLine 2\nLine 3"
fs.writeFile("data.txt", data)

appendFile(path, content)

Append string to file.

fs.appendFile("log.txt", "New entry\n")

writeBytes(path, bytes)

Write byte array to file.

let bytes = [0x48, 0x65, 0x6C, 0x6C, 0x6F]  // "Hello"
fs.writeBytes("binary.dat", bytes)

writeLines(path, lines)

Write array of lines to file.

let lines = ["First line", "Second line", "Third line"]
fs.writeLines("output.txt", lines)

File Existence and Info

exists(path)

Check if file or directory exists.

if (fs.exists("config.json")) {
let config = fs.readFile("config.json")
} else {
io.println("Config file not found")
}

isFile(path)

Check if path is a regular file.

if (fs.isFile("data.txt")) {
io.println("It's a file")
}

isDir(path)

Check if path is a directory.

if (fs.isDir("src")) {
io.println("It's a directory")
}

size(path)

Get file size in bytes.

let bytes = fs.size("large_file.bin")
let mb = bytes / (1024 * 1024)
io.println("Size: " + mb + " MB")

stat(path)

Get detailed file information.

let info = fs.stat("file.txt")
io.println("Size: " + info["size"])
io.println("Modified: " + info["modified"])
io.println("Is Directory: " + info["isDir"])

File Operations

copy(source, dest)

Copy a file.

fs.copy("original.txt", "backup.txt")

move(source, dest)

Move or rename a file.

fs.move("old_name.txt", "new_name.txt")
fs.move("file.txt", "archive/file.txt")

remove(path)

Delete a file.

fs.remove("temp.txt")

rename(oldPath, newPath)

Rename a file (alias for move).

fs.rename("old.txt", "new.txt")

Directory Operations

mkdir(path)

Create a directory.

fs.mkdir("new_folder")

mkdirp(path)

Create directory and all parent directories.

fs.mkdirp("path/to/deep/folder")

rmdir(path)

Remove an empty directory.

fs.rmdir("empty_folder")

rmrf(path)

Remove directory and all contents (recursive).

fs.rmrf("build")  // Careful! Deletes everything

listDir(path)

List directory contents.

let files = fs.listDir(".")
for (let i = 0; i < len(files); i = i + 1) {
io.println(files[i])
}

glob(pattern)

Find files matching a pattern.

let sources = fs.glob("src/*.qz")
let allQz = fs.glob("**/*.qz")  // Recursive

Path Operations

join(...parts)

Join path components.

let path = fs.join("src", "core", "main.qz")
// "src/core/main.qz" (Unix) or "src\core\main.qz" (Windows)

dirname(path)

Get directory name.

fs.dirname("/home/user/file.txt")   // "/home/user"

basename(path)

Get file name.

fs.basename("/home/user/file.txt")  // "file.txt"

extname(path)

Get file extension.

fs.extname("file.txt")    // ".txt"
fs.extname("archive.tar.gz") // ".gz"

absolute(path)

Get absolute path.

let abs = fs.absolute("./file.txt")
// "/home/user/project/file.txt"

normalize(path)

Normalize path (resolve . and ..).

fs.normalize("src/../lib/./file.txt")  // "lib/file.txt"

Working Directory

cwd()

Get current working directory.

let dir = fs.cwd()
io.println("Working in: " + dir)

chdir(path)

Change working directory.

fs.chdir("/home/user/project")

Temporary Files

tempDir()

Get system temp directory.

let tmp = fs.tempDir()  // "/tmp" or "C:\Users\...\Temp"

tempFile(prefix)

Create a temporary file.

let tmpFile = fs.tempFile("myapp_")
fs.writeFile(tmpFile, "temp data")
// ... use temp file ...
fs.remove(tmpFile)

Examples

Safe File Read

import system.fs as fs
import system.io as io

fn safeReadFile(path) {
if (!fs.exists(path)) {
io.eprintln("Error: File not found: " + path)
return null
}

try {
return fs.readFile(path)
} catch (e) {
io.eprintln("Error reading file: " + e.message)
return null
}
}

Find All Source Files

import system.fs as fs

fn findSources(dir) {
let result = []
let entries = fs.listDir(dir)

for (let i = 0; i < len(entries); i = i + 1) {
let path = fs.join(dir, entries[i])

if (fs.isDir(path)) {
let subFiles = findSources(path)
// Add subFiles to result
for (let j = 0; j < len(subFiles); j = j + 1) {
result[len(result)] = subFiles[j]
}
} else if (fs.extname(path) == ".qz") {
result[len(result)] = path
}
}

return result
}

let sources = findSources("src")
io.println("Found " + len(sources) + " source files")

Backup File

import system.fs as fs
import system.time as time

fn backupFile(path) {
if (!fs.exists(path)) {
return false
}

let timestamp = time.format(time.now(), "%Y%m%d_%H%M%S")
let ext = fs.extname(path)
let base = fs.basename(path)
base = str.substring(base, 0, str.length(base) - str.length(ext))

let backupPath = base + "_" + timestamp + ext
fs.copy(path, backupPath)

return true
}

backupFile("important.txt")
// Creates: important_20240115_143022.txt

Next: system.json →