DirTree Zig Reference Documentation
DirTree
Current Version: 11.6.1
Chilkat.DirTree
Iterate through the contents of a base directory, either recursively or
one level at a time.
Get the current item's full path, relative path, UNC path, size, and
directory/file status.
Traverse entire directory trees when an application needs to process all
nested files and folders.
Limit iteration to the immediate contents of the base directory when
deeper traversal is not needed.
Use absolute or relative paths depending on whether the application needs
filesystem access or archive-style relative names.
Use
For an extended overview, see
DirTree Class Overview.
Iterate through files and subdirectories under a base directory.
Chilkat.DirTree is a lightweight directory-tree iterator for
traversing files and subdirectories under a selected base directory. It
supports recursive and non-recursive traversal and exposes useful information
about the current item, including absolute path, UNC path, relative path,
file size, and whether the current item is a directory.
Directory traversal
Current-item details
Recursive scanning
Non-recursive listing
Path-friendly output
Error-aware iteration
DoneIterating to distinguish normal completion from an
error when an iteration method returns false.
DoneIterating when iteration stops to determine whether traversal
completed normally or ended because of an error.
Object Creation
// Add the package once:
// zig fetch --save https://chilkatdownload.com/11.6.1/chilkat-zig-11.6.1.tar.gz
// and in build.zig:
// const chilkat = b.dependency("chilkat", .{ .target = target, .optimize = optimize });
// exe.root_module.addImport("chilkat", chilkat.module("chilkat"));
const chilkat = @import("chilkat");
// Once per process, before any other Chilkat call:
try chilkat.unlockBundle("Anything for 30-day trial");
const dir_tree = try chilkat.DirTree.init();
defer dir_tree.deinit();Creates the underlying native Chilkat object. DirTree is a one-pointer struct passed by value; copying it copies the handle (two names for one object). Returns error.OutOfMemory if the library could not allocate the object. Use an object from one thread at a time; it may be handed from one thread to another.
Releases the native object. Call it exactly once per object (usually with defer); the handle is invalid afterwards. Objects returned by methods are owned by the caller too and are released the same way.
Wraps a handle obtained from the C API (chilkat.c.CkDirTree), taking ownership of it. The struct's handle field goes the other way, for anything the Zig API does not cover.
Errors and memory
Methods that can fail return an error union: a method whose only outcome is success or failure returns Error!void; a method producing a string returns (Error || Allocator.Error)![:0]u8; a method producing an object returns Error!T. chilkat.Error is error{ChilkatFailed}; the reason for a failure is in getLastErrorText, and the object remains usable. Properties never fail, and methods that answer a question (hasMember, isUnlocked, ...) return a plain bool.
String arguments are [:0]const u8 (UTF-8; string literals can be passed as is). String results are copies allocated with the allocator argument and owned by the caller, so they stay valid across later calls on the same object.
const text = dir_tree.someMethod(allocator, ...) catch |err| {
const why = try dir_tree.getLastErrorText(allocator);
defer allocator.free(why);
std.debug.print("{s}\n", .{why});
return err;
};
defer allocator.free(text);
Properties
BaseDir
pub fn getBaseDir(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
pub fn setBaseDir(self: DirTree, value: [:0]const u8) void
Specifies the root directory whose contents are traversed. Set this property before calling BeginIterate.
The value may be an absolute filesystem path or a relative path. A relative path is resolved from the calling process's current working directory.
Recurse property determines whether nested levels are included.
DebugLogFilePath
pub fn getDebugLogFilePath(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
pub fn setDebugLogFilePath(self: DirTree, value: [:0]const u8) void
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
DoneIterating
pub fn getDoneIterating(self: DirTree) bool
Indicates whether the traversal has reached its normal end and no current item remains.
This property is especially important because both BeginIterate and AdvancePosition can return false for either normal completion or an error:
| Method result | DoneIterating |
Meaning |
|---|---|---|
false |
true |
The directory was empty, or the last item has been passed. This is normal completion. |
false |
false |
An error occurred. Examine LastErrorText. |
FileSize32
pub fn getFileSize32(self: DirTree) i32
Returns the size of the current file in bytes as a 32-bit integer. The value is 0 when the current item is a directory.
FileSize64 when the language binding provides it, or FileSizeStr when a decimal string is more portable.
FileSize64
pub fn getFileSize64(self: DirTree) i64
Returns the size of the current file in bytes as a 64-bit integer. The value is 0 when the current item is a directory.
This is the preferred numeric size property for files that may exceed the range of FileSize32. In bindings that do not expose a 64-bit integer property, use FileSizeStr.
FileSizeStr
pub fn getFileSizeStr(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Returns the size of the current file in bytes as an unsigned decimal string. The value is 0 when the current item is a directory.
This representation is useful for very large files and for language bindings where a 64-bit integer is unavailable or inconvenient. Convert the string to an appropriate arbitrary-precision or 64-bit numeric type only when arithmetic is required.
topFullPath
pub fn getFullPath(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Returns the absolute filesystem path of the current file or subdirectory, including the item's name.
The path uses the native path syntax for the operating system. For a path relative to BaseDir, use RelativePath.
FullUncPath
pub fn getFullUncPath(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Returns the current item's Windows extended-length absolute path. For example, a local path may be returned as \\?\C:\data\reports\file.txt; a network path may use the \\?\UNC\server\share\... form.
\\?\ prefix asks Windows to use extended-length path parsing and is useful when working with paths that may exceed legacy path-length limits. Use FullPath when an ordinary native absolute path is preferred.
IsDirectory
pub fn getIsDirectory(self: DirTree) bool
Returns true when the current item is a subdirectory and false when it is a file.
Check this property before using a file-size property. Directory items report a size of 0.
LastErrorHtml
pub fn getLastErrorHtml(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
pub fn getLastErrorText(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
pub fn getLastErrorXml(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
pub fn getLastMethodSuccess(self: DirTree) bool
pub fn setLastMethodSuccess(self: DirTree, value: bool) void
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
Recurse
pub fn getRecurse(self: DirTree) bool
pub fn setRecurse(self: DirTree, value: bool) void
Controls whether traversal descends into nested subdirectories.
true |
Visit files and directories throughout the complete tree rooted at BaseDir. |
|---|---|
false |
Visit only the immediate contents of BaseDir. |
The default value is true. Set this property before calling BeginIterate.
RelativePath
pub fn getRelativePath(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Returns the path of the current file or subdirectory relative to BaseDir, including the item's name.
For example, if BaseDir is C:\data and the current item is C:\data\images\logo.png, this property returns images\logo.png on Windows. Path separators follow the operating system's native convention.
UncommonOptions
pub fn getUncommonOptions(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
pub fn setUncommonOptions(self: DirTree, value: [:0]const u8) void
Provides a comma-separated list of specialized compatibility options. The default is the empty string, and applications should normally leave it unchanged.
| Keyword | Effect |
|---|---|
Q_EncodePaths |
Returns path-valued output properties in Chilkat's Q-encoded representation. Introduced in Chilkat v11.1.0. This changes only the returned string representation; it does not change which filesystem items are traversed. |
VerboseLogging
pub fn getVerboseLogging(self: DirTree) bool
pub fn setVerboseLogging(self: DirTree, value: bool) void
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
pub fn getVersion(self: DirTree, allocator: Allocator) Allocator.Error![:0]u8
Methods
AdvancePosition
Advances the traversal from the current item to the next file or subdirectory.
If the method returns true, the current-item properties—such as FullPath, RelativePath, IsDirectory, and the file-size properties—describe the newly selected item.
| Return value | DoneIterating |
Meaning |
|---|---|---|
true |
false |
The iterator advanced successfully and a current item is available. |
false |
true |
The last item has been passed; traversal completed normally. |
false |
false |
An error occurred. Examine LastErrorText. |
Returns error.ChilkatFailed on failure; getLastErrorText explains why.
BeginIterate
Initializes a new traversal using the current BaseDir and Recurse settings.
If the method returns true, the iterator is positioned at the first file or subdirectory, and the current-item properties can be read immediately. Continue by processing that item and then calling AdvancePosition.
| Return value | DoneIterating |
Meaning |
|---|---|---|
true |
false |
The first item is available. |
false |
true |
The directory contains no items in the selected traversal scope. This is not an error. |
false |
false |
Initialization failed. Examine LastErrorText. |
BeginIterate or AdvancePosition.
Returns error.ChilkatFailed on failure; getLastErrorText explains why.