xDir Library - Customizing

The xDir events and properties can be used to implement your own search criteria. Suppose that you need to find all files containing the string "photo" in their names or contents. Obviously, if a file contains the word "photo", there is no need to check its contents. The implementation of this logic with xDir, which can be used instead of the intrinsic VBA Dir function, is trivial.

Suppose we have a form with a ListBox named lstResults to display the sought filenames when the user presses the cmdSearch command button. The corresponding xDir code will look like the following:

Implements IXDItemCheck
Implements IXDFoundItem

Private Sub cmdSearch_Click()
   Dim xd As New xDir
   xd.RootFolder = ".\TestFolder\"
   xd.FileCriteria.Context.SearchPhrases = "photo"
   xd.ProcessFolder Me
End Sub

Private Sub IXDItemCheck_Proc(ByVal ParentFullPath As String, _
      ByVal ItemName As String, ByVal ItemType As EItemTypes, _
      SkipItem As Boolean, CancelEnum As Boolean, _
      ByVal UserValue As Long)
   If LCase$(ItemName) Like "*photo*" Then
      lstResults.AddItem ParentFullPath & ItemName
      SkipItem = True
   End If
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)
   lstResults.AddItem ParentFullPath & ItemName
End Sub

The key point of this code is that we must check filenames yourself, and force xDir to check file contents only. We check the filename in the IXDItemCheck_Proc sub and ignore further search in the file contents by assigning True to the SkipItem parameter passed by reference. If we specified the required file mask with the statement

xd.FileCriteria.Mask = "*photo*"

, xDir would not search the word "photo" inside a file whose name does not contain this word because xDir checks the file mask first and searches the specified word in the file contents only if the filename matches the specified mask. Actually we would search for all files containging the word "photo" both in filenames and contents.

As you can see from this example, xDir provides you with a lot of useful events and properties that allow you to implement your own file/folder search operations.

What's New in xDir »