Dir API
Directory I/O through static path helpers and directory handles, with entry queries, mutations, and two styles of enumeration.
Overview
The global identifier Dir is a namespace of static helpers and opener constructors. Static helpers operate on a path directly, without opening anything. Opening a directory returns a directory handle whose instance methods are invoked as d.method(...); the handle carries a current directory, listing filters, and iterator state.
Conventions
See also the site-wide CLI conventions. The points below are specific to Dir:
- Paths. Strings are interpreted as filesystem paths, absolute or relative to the current working directory. No virtual-filesystem prefixes are applied.
- Names vs paths. Instance methods that take a
nameexpect a relative entry under the handle's current directory. Methods that take apathaccept either an absolute path or a path relative to the current directory. - Numbers. Counts, indices, and byte sizes are Zym numbers. Integer arguments are truncated toward zero.
- Booleans. Most mutating operations return
trueon success,falseon failure. Query methods return a bool directly. - Lists. Methods that enumerate entries (
files,directories) return Zym lists of strings. Order is filesystem-dependent and not guaranteed. - Open failures.
openandopenTempreturnnullon failure — check withif (d == null). - Errors. Invalid argument types, or instance methods called on a closed or invalid handle, produce a Zym runtime error of the form
Dir.method(args) ....
Opening Directories
Opens the directory at path and returns a directory handle. Returns null on failure.
path(string) — absolute path, or path relative to the current working directory
Creates a new temporary directory and returns a handle to it, or null on failure. prefix is a leaf-name prefix for the generated directory. When keep is false the directory is cleaned up when the handle is released; when true it is left in place.
prefix(string) — leaf-name prefix for the generated directorykeep(boolean) —trueto leave the directory in place after the handle is released
var d = Dir.open("src") if (d == null) { return } // open failed var t = Dir.openTemp("zym_", false) // removed when handle is released
Static Helpers
Convenience wrappers that operate on a path without opening a handle.
Returns true if path resolves to an existing directory.
Creates a single directory at path. The parent directory must already exist. Returns true on success.
Creates the directory at path and any missing parents. Returns true on success.
Copies a single file at src to dst. Returns true on success.
src(string) — path of the file to copydst(string) — destination path
Renames or moves src to dst. Returns true on success.
Removes a file or an empty directory at path. Refuses to delete a non-empty directory. Returns true on success.
Returns the names of regular files directly under path as a list of strings.
Returns the names of subdirectories directly under path as a list of strings.
Returns the number of logical drives. Platform-dependent; 0 on Unix-likes.
Returns the name of drive idx; empty string if idx is out of range.
idx(number) — drive index
Dir.remove only removes empty directories. To clear a directory tree, open a handle and call eraseContentsRecursive(), or pair Dir.remove with a recursive enumeration of files() / directories().
if (!Dir.exists("build")) { Dir.makeDir("build") } Dir.makeDirRecursive("build/out/logs") // creates any missing parents Dir.copy("config.zym", "build/config.zym") Dir.rename("build/out", "build/dist")
Handle State
Returns the absolute path of the handle's current directory.
Navigates the handle to path; resolves . and ... Returns false if the target doesn't exist.
path(string) — absolute path, or path relative to the current directory
Queries on Entries
Entry-relative queries. name is resolved against the handle's current directory.
Returns true if name is a regular file. Follows symlinks.
Returns true if name is a directory. Follows symlinks.
Readability query for name. Returns a bool.
Writability query for name. Returns a bool.
Returns true if name is a symbolic link. Reports the link itself — the link is not followed.
Returns the target path of a symbolic link; empty string if name is not a link.
Returns the number of free bytes on the filesystem containing the current directory.
if (d.isLink("current")) { print(d.readLink("current")) // target of the link } var free = d.spaceLeft() // free bytes on this filesystem
Mutations
Creates a single directory under the current directory, or at an absolute path. Returns true on success.
Creates the directory and any missing parents. Returns true on success.
Copies a single file. Returns true on success.
Renames or moves an entry. Returns true on success.
Removes a file or an empty directory. Refuses to delete a non-empty directory. Returns true on success.
Recursively deletes every entry inside the current directory; the directory itself is kept. Returns true on success.
eraseContentsRecursive is non-recoverable — entries are not moved to a trash or recycle bin. Confirm d.path() before calling it.
Creates a symbolic link at dst pointing at src. Returns true on success. May fail without elevated privileges on Windows; on Unix-likes symlink creation is unprivileged.
src(string) — path the link points atdst(string) — path of the link to create
var scratch = Dir.open("build/tmp") if (scratch != null) { scratch.eraseContentsRecursive() // contents gone, build/tmp kept }
Enumeration
Two styles are supported. Pick whichever fits the script.
Snapshot Style
Returns the whole listing as a list of names.
Returns the names of regular files in the current directory as a list of strings.
Returns the names of subdirectories in the current directory as a list of strings.
Iterator Style
Walks one entry at a time and exposes per-entry metadata without allocating a list. Between listBegin and listEnd, each call to listNext advances the iterator, and listCurrentIsDir / listCurrentIsHidden then reflect the entry just returned.
Starts iteration. Returns false if the directory can't be opened.
Returns the next entry name, or null when enumeration is finished.
Returns true if the entry returned by the last listNext is a directory.
Returns true if the entry is hidden (leading . on Unix, hidden attribute on Windows).
Releases iterator state; returns null. Safe to call multiple times, and even after listNext returned null. Always pair with listBegin — leaving an iterator open holds a directory handle at the OS level.
if (d.listBegin()) { var name = d.listNext() while (name != null) { if (d.listCurrentIsDir()) { print("dir : %s", name) } else { print("file: %s", name) } name = d.listNext() } d.listEnd() }
Listing Filters
Filters apply to both snapshot and iterator enumeration.
If true, . and .. are included in listings. Default: false. Returns null.
If true, hidden entries are included in listings. Default: false. Returns null.
Returns the current navigational-entries setting.
Returns the current hidden-entries setting.
d.setIncludeHidden(true) var all = d.files() // now includes dotfiles
Filesystem Metadata
Returns whether names under path are case-sensitive. Platform- and filesystem-dependent.
Returns the platform name of the filesystem hosting the current directory (e.g. "ext4", "NTFS", "APFS"). Empty string if unavailable.
Drives
Returns the number of logical drives.
Returns the name of drive idx.
Returns the index of the drive backing the current directory.
driveCount() is 0 and drive(idx) returns an empty string.
Notes & Gotchas
Handle aliasing. Assigning a Dir handle (d2 = d1) aliases the underlying directory; operations on either reference affect the same position and filter state. Open a second handle with Dir.open for independent iteration.
Closing is implicit. The directory handle is released when the last reference is dropped. There is no explicit close(); terminate iterators with listEnd if the handle is kept alive.
Symlinks. fileExists and dirExists follow symlinks; isLink reports the link itself. Use readLink to get the target.
Hidden and navigational entries are filtered by default. ., .., and hidden names do not appear in files() / directories() / listNext() unless enabled via setIncludeNavigational(true) / setIncludeHidden(true).
Examples
Listing a Directory
var d = Dir.open(".") if (d == null) { print("cannot open cwd") } else { d.setIncludeHidden(false) var dirs = d.directories() for (var i = 0; i < length(dirs); i = i + 1) { print("dir : %s", dirs[i]) } var files = d.files() for (var i = 0; i < length(files); i = i + 1) { print("file: %s", files[i]) } }
Temporary Workspace
if (!Dir.exists("build/tmp")) { Dir.makeDirRecursive("build/tmp") } var t = Dir.openTemp("zym_", false) // auto-cleaned when handle drops t.makeDir("cache")