DirTree SQL Server Reference Documentation

DirTree

Current Version: 11.6.0

Chilkat.DirTree

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.

Object Creation

DECLARE @hr int
DECLARE @dirTree int
EXEC @hr = sp_OACreate 'Chilkat.DirTree', @dirTree OUT
IF @hr <> 0
BEGIN
    PRINT 'Failed to create ActiveX component'
    RETURN
END

-- ... use @dirTree ...

EXEC @hr = sp_OADestroy @dirTree

T-SQL uses the Chilkat ActiveX through the OLE Automation stored procedures. They must be enabled once on the server (EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;), and the Chilkat ActiveX registered must match the bitness of the SQL Server instance (64-bit for a 64-bit SQL Server). To bind to a specific major version of Chilkat, append the major version number to the ProgID, such as sp_OACreate 'Chilkat.DirTree.11' for Chilkat v11.*.*.

sp_OACreate returns an object token (an int) that is passed as the first argument of every sp_OAMethod, sp_OAGetProperty and sp_OASetProperty call, and released with sp_OADestroy. Objects returned by methods (such as an HttpResponse or JsonObject) are also tokens received through an int OUT parameter; they must likewise be destroyed, and the OUT parameter is NULL when the method fails to return an object. Objects passed as arguments are passed by their token. When an OLE Automation procedure itself fails (non-zero @hr), sp_OAGetErrorInfo describes the error.

Data types: strings are nvarchar(4000); integers, booleans (1 or 0) and object tokens are int; dates are datetime. In the signatures on this page, @success, @iResult, @sResult and the like are the OUT variables receiving a method's return value, @iValue / @sValue receive or supply a property value, and the remaining @ variables are the method's arguments in order.

A string returned through an OUT parameter is limited to 4000 characters. For longer values, retrieve the result as a result set into a table variable instead of an OUT parameter, for example DECLARE @tmp TABLE (outputLine ntext) followed by INSERT INTO @tmp EXEC sp_OAGetProperty @dirTree, 'LastErrorText'. See string length limitations for strings returned by sp_OAMethod calls.

Methods that pass or return raw byte arrays are not shown on this page, because varbinary(max) values cannot be exchanged through sp_OAMethod (see varbinary(max) limitation). Use the BinData-based alternatives (methods ending in Bd) or the base64 / hex string-encoded variants instead. Binary properties (such as LastBinaryResult) can be retrieved as a result set into a table variable, as shown in their signatures. Asynchronous (*Async) methods and event callbacks are not available from SQL Server.

Properties

BaseDir
EXEC sp_OAGetProperty @dirTree, 'BaseDir', @sValue OUT
EXEC sp_OASetProperty @dirTree, 'BaseDir', @sValue

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.

Iteration scope: The iterator visits the files and subdirectories contained under this directory. The Recurse property determines whether nested levels are included.

top
DebugLogFilePath
EXEC sp_OAGetProperty @dirTree, 'DebugLogFilePath', @sValue OUT
EXEC sp_OASetProperty @dirTree, 'DebugLogFilePath', @sValue

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.

More Information and Examples
top
DoneIterating
EXEC sp_OAGetProperty @dirTree, 'DoneIterating', @iValue OUT

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 0 for either normal completion or an error:

Method result DoneIterating Meaning
0 1 The directory was empty, or the last item has been passed. This is normal completion.
0 0 An error occurred. Examine LastErrorText.

top
FileSize32
EXEC sp_OAGetProperty @dirTree, 'FileSize32', @iValue OUT

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.

Large files: A 32-bit integer cannot represent all modern file sizes. Use FileSize64 when the language binding provides it, or FileSizeStr when a decimal string is more portable.

top
FileSizeStr
EXEC sp_OAGetProperty @dirTree, 'FileSizeStr', @sValue OUT
Introduced in version 9.5.0.88

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.

top
FullPath
EXEC sp_OAGetProperty @dirTree, 'FullPath', @sValue OUT

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.

top
FullUncPath
EXEC sp_OAGetProperty @dirTree, 'FullUncPath', @sValue OUT

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. Use FullPath when an ordinary native absolute path is preferred.

top
IsDirectory
EXEC sp_OAGetProperty @dirTree, 'IsDirectory', @iValue OUT

Returns 1 when the current item is a subdirectory and 0 when it is a file.

Check this property before using a file-size property. Directory items report a size of 0.

top
LastBinaryResult
INSERT INTO @tmp EXEC sp_OAGetProperty @dirTree, 'LastBinaryResult'

This property is mainly used in SQL Server stored procedures to retrieve binary data from the last method call that returned binary data. It is only accessible if Chilkat.Global.KeepBinaryResult is set to 1. This feature allows for the retrieval of large varbinary results in an SQL Server environment, which has restrictions on returning large data via method calls, though temp tables can handle binary properties.

top
LastErrorHtml
EXEC sp_OAGetProperty @dirTree, 'LastErrorHtml', @sValue OUT

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.

top
LastErrorText
EXEC sp_OAGetProperty @dirTree, 'LastErrorText', @sValue OUT

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.

top
LastErrorXml
EXEC sp_OAGetProperty @dirTree, 'LastErrorXml', @sValue OUT

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.

top
LastMethodSuccess
EXEC sp_OAGetProperty @dirTree, 'LastMethodSuccess', @iValue OUT
EXEC sp_OASetProperty @dirTree, 'LastMethodSuccess', @iValue

Indicates the success or failure of the most recent method call: 1 means success, 0 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.

top
LastStringResult
EXEC sp_OAGetProperty @dirTree, 'LastStringResult', @sValue OUT

In SQL Server stored procedures, this property holds the string return value of the most recent method call that returns a string. It is accessible only when Chilkat.Global.KeepStringResult is set to TRUE. SQL Server has limitations on string lengths returned from methods and properties, but temp tables can be used to access large strings.

top
LastStringResultLen
EXEC sp_OAGetProperty @dirTree, 'LastStringResultLen', @iValue OUT

The length, in characters, of the string contained in the LastStringResult property.

top
Recurse
EXEC sp_OAGetProperty @dirTree, 'Recurse', @iValue OUT
EXEC sp_OASetProperty @dirTree, 'Recurse', @iValue

Controls whether traversal descends into nested subdirectories.

1 Visit files and directories throughout the complete tree rooted at BaseDir.
0 Visit only the immediate contents of BaseDir.

The default value is 1. Set this property before calling BeginIterate.

top
RelativePath
EXEC sp_OAGetProperty @dirTree, 'RelativePath', @sValue OUT

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.

top
UncommonOptions
EXEC sp_OAGetProperty @dirTree, 'UncommonOptions', @sValue OUT
EXEC sp_OASetProperty @dirTree, 'UncommonOptions', @sValue
Introduced in version 11.1.0

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.

top
VerboseLogging
EXEC sp_OAGetProperty @dirTree, 'VerboseLogging', @iValue OUT
EXEC sp_OASetProperty @dirTree, 'VerboseLogging', @iValue

If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
EXEC sp_OAGetProperty @dirTree, 'Version', @sValue OUT

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AdvancePosition
EXEC sp_OAMethod @dirTree, 'AdvancePosition', @success OUT

Advances the traversal from the current item to the next file or subdirectory.

If the method returns 1, the current-item properties—such as FullPath, RelativePath, IsDirectory, and the file-size properties—describe the newly selected item.

Return value DoneIterating Meaning
1 0 The iterator advanced successfully and a current item is available.
0 1 The last item has been passed; traversal completed normally.
0 0 An error occurred. Examine LastErrorText.

Returns 1 for success, 0 for failure.

top
BeginIterate
EXEC sp_OAMethod @dirTree, 'BeginIterate', @success OUT

Initializes a new traversal using the current BaseDir and Recurse settings.

If the method returns 1, 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
1 0 The first item is available.
0 1 The directory contains no items in the selected traversal scope. This is not an error.
0 0 Initialization failed. Examine LastErrorText.
Property lifetime: Treat the current-item properties as valid only while the iterator is positioned on an item—that is, after a successful BeginIterate or AdvancePosition.

Returns 1 for success, 0 for failure.

top