VBA Dir Function Alternative - xDir
10Tec xDir is an ActiveX library that can be considered as an excellent alternative to the VBA Dir function. The library name is a shortening for "extended Dir" implying a wider range of features compared to the VBA Dir function. xDir can successfully replace the Dir function not only in Microsoft Office VBA (Word, Excel and Access), but also the Dir function in VB6.
Thanks to xDir, developers can easily enumerate files and folders in the specified folder and all its subfolders and perform related tasks. Among them filtering folder and file list using various filter criteria and searching file contents for words or phrases in the ANSI and Unicode encodings. Though xDir is a non-visual component, it can greatly help with building a visual tree of folders and files:
Coding with xDir is very simple. Your code looks straightforward and much cleaner compared to an equivalent solution based on the VBA Dir function. To do something with xDir, create an instance of the xDir
object, implement a callback function to receive notifications about found items and launch an enumeration using the ProcessFolder
method. Below is an example of xDir code to enumerate all files in the C:\WINDOWS folder and all its nested subfolders:
Implements IXDFoundItem
Private Sub Command1_Click()
Dim xd As New xDir
xd.RootFolder = "C:\WINDOWS"
xd.ProcessFolder Me
End Sub
Private Sub IXDFoundItem_Proc(ByVal ParentFullPath As String, _
ByVal ItemName As String, ByVal ItemType As EItemTypes, _
CancelEnum As Boolean, ByVal UserValue As Long)
List1.AddItem ParentFullPath & ItemName
End Sub
xDir works super-fast as its file/folder functionality is based on efficient Windows API calls. For instance, xDir enumerated all files in the C:\WINDOWS folder and all its subfolders (120'696 files) in about 3 seconds on a test PC with an Intel Core i5 processor and Windows 10 Professional 64-bit.
xDir is available in two editions - Standard and Professional. The Standard edition is an ActiveX DLL and allows you to gain the highest performance. The Professional edition has been implemented as an ActiveX-exe server and provides developers with the ability to enumerate files and folders in a background process using asynchronous mode without blocking the user interface.
Read other pages in the product section to find out more about advantages of xDir over the VBA Dir function (sort by names, cancelling search, etc.)