Class DirTree
- All Implemented Interfaces:
AutoCloseable
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
Iterate through the contents of a base directory, either recursively or one level at a time.
Current-item details
Get the current item's full path, relative path, UNC path, size, and directory/file status.
Recursive scanning
Traverse entire directory trees when an application needs to process all nested files and folders.
Non-recursive listing
Limit iteration to the immediate contents of the base directory when deeper traversal is not needed.
Path-friendly output
Use absolute or relative paths depending on whether the application needs filesystem access or archive-style relative names.
Error-aware iteration
Use DoneIterating to distinguish normal completion from an
error when an iteration method returns false.
Common pattern:
Set the base directory and recursion behavior, begin iterating, process each
current item using the path and metadata properties, then check
DoneIterating when iteration stops to determine whether traversal
completed normally or ended because of an error.
For an extended overview, see DirTree Class Overview.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdvances the traversal from the current item to the next file or subdirectory.voidInitializes a new traversal using the currentBaseDirandRecursesettings.Specifies the root directory whose contents are traversed.booleanIndicates whether the traversal has reached its normal end and no current item remains.intReturns the size of the current file in bytes as a 32-bit integer.longReturns the size of the current file in bytes as a 64-bit integer.Returns the size of the current file in bytes as an unsigned decimal string.Returns the absolute filesystem path of the current file or subdirectory, including the item's name.Returns the current item's Windows extended-length absolute path.booleanReturnstruewhen the current item is a subdirectory andfalsewhen it is a file.booleanControls whether traversal descends into nested subdirectories.Returns the path of the current file or subdirectory relative toBaseDir, including the item's name.Provides a comma-separated list of specialized compatibility options.voidsetBaseDir(String value)Sets theBaseDirproperty; seegetBaseDir().voidsetRecurse(boolean value)Sets theRecurseproperty; seegetRecurse().voidsetUncommonOptions(String value)Sets theUncommonOptionsproperty; seegetUncommonOptions().Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
DirTree
public DirTree()Creates a newDirTreeobject.
-
-
Method Details
-
getBaseDir
Specifies the root directory whose contents are traversed. Set this property before callingBeginIterate.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.
Iteration scope: The iterator visits the files and subdirectories contained under this directory. The
Recurseproperty determines whether nested levels are included.- Returns:
- the value of the
BaseDirproperty
-
setBaseDir
Sets theBaseDirproperty; seegetBaseDir().- Parameters:
value- the new value (notnull)
-
getDoneIterating
public boolean getDoneIterating()Indicates whether the traversal has reached its normal end and no current item remains.This property is especially important because both
BeginIterateandAdvancePositioncan returnfalsefor either normal completion or an error:Method result
DoneIteratingMeaningfalsetrueThe directory was empty, or the last item has been passed. This is normal completion.falsefalseAn error occurred. ExamineLastErrorText.- Returns:
- the value of the
DoneIteratingproperty (read-only)
-
getFileSize32
public int getFileSize32()Returns the size of the current file in bytes as a 32-bit integer. The value is0when the current item is a directory.Large files: A 32-bit integer cannot represent all modern file sizes. Use
FileSize64when the language binding provides it, orFileSizeStrwhen a decimal string is more portable.- Returns:
- the value of the
FileSize32property (read-only)
-
getFileSize64
public long getFileSize64()Returns the size of the current file in bytes as a 64-bit integer. The value is0when 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, useFileSizeStr.- Returns:
- the value of the
FileSize64property (read-only)
-
getFileSizeStr
Returns the size of the current file in bytes as an unsigned decimal string. The value is0when 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.
- Returns:
- the value of the
FileSizeStrproperty (read-only)
-
getFullPath
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, useRelativePath.- Returns:
- the value of the
FullPathproperty (read-only)
-
getFullUncPath
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.Windows path form: The
\\?\prefix asks Windows to use extended-length path parsing and is useful when working with paths that may exceed legacy path-length limits. UseFullPathwhen an ordinary native absolute path is preferred.- Returns:
- the value of the
FullUncPathproperty (read-only)
-
getIsDirectory
public boolean getIsDirectory()Returnstruewhen the current item is a subdirectory andfalsewhen it is a file.Check this property before using a file-size property. Directory items report a size of
0.- Returns:
- the value of the
IsDirectoryproperty (read-only)
-
getRecurse
public boolean getRecurse()Controls whether traversal descends into nested subdirectories.trueVisit files and directories throughout the complete tree rooted atBaseDir.falseVisit only the immediate contents ofBaseDir.The default value is
true. Set this property before callingBeginIterate.- Returns:
- the value of the
Recurseproperty
-
setRecurse
public void setRecurse(boolean value)Sets theRecurseproperty; seegetRecurse().- Parameters:
value- the new value
-
getRelativePath
Returns the path of the current file or subdirectory relative toBaseDir, including the item's name.For example, if
BaseDirisC:\dataand the current item isC:\data\images\logo.png, this property returnsimages\logo.pngon Windows. Path separators follow the operating system's native convention.- Returns:
- the value of the
RelativePathproperty (read-only)
-
getUncommonOptions
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_EncodePathsReturns 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.- Returns:
- the value of the
UncommonOptionsproperty
-
setUncommonOptions
Sets theUncommonOptionsproperty; seegetUncommonOptions().- Parameters:
value- the new value (notnull)
-
advancePosition
public void advancePosition()Advances the traversal from the current item to the next file or subdirectory.If the method returns
true, the current-item properties—such asFullPath,RelativePath,IsDirectory, and the file-size properties—describe the newly selected item.Return value
DoneIteratingMeaningtruefalseThe iterator advanced successfully and a current item is available.falsetrueThe last item has been passed; traversal completed normally.falsefalseAn error occurred. ExamineLastErrorText.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
beginIterate
public void beginIterate()Initializes a new traversal using the currentBaseDirandRecursesettings.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 callingAdvancePosition.Return value
DoneIteratingMeaningtruefalseThe first item is available.falsetrueThe directory contains no items in the selected traversal scope. This is not an error.falsefalseInitialization failed. ExamineLastErrorText.Property lifetime: Treat the current-item properties as valid only while the iterator is positioned on an item—that is, after a successful
BeginIterateorAdvancePosition.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-