Visual Basic File API Routines

FindFirstFile: Recursive Search for Folders Using a Folder Mask (minimal code)
     
Posted:   Wednesday February 27, 2002
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP Pro
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

FindFirstFile: Recursive File Search for Single or Multiple File Types (minimal code)
FindFirstFile: Recursive File Search Including/Excluding Single or Multiple File Types (minimal code)
FindFirstFile: Recursive Search for Folders Using a Folder Mask (minimal code)
FindFirstFile: Recursive File Search (minimal code)
FindFirstFile: Recursive Search for Folders (minimal code)
FindFirstFile: Changing File and/or Folder Attributes Recursively
FindFirstFile: Fast Directory File Count
FindFirstFile: Extract Filename from a Full Path
FindFirstFile: Performance Comparison - FSO vs. API
FindFirstFile: Comparison of FindFirstFile and SearchTreeForFile
FindFirstFile: Save a Recursive Search of All Drives to Disk
FindFirstFile: Save a Recursive Search of Specified Drives to Disk
GetFileVersionInfo: File Search and File Property Info
     
 Prerequisites
None.

Here's a demo, similar to the other multiple file type demos listed above, that shows how to use the PathMatchSpec API along with FindFirstFile / FindNextFile to perform a recursive search across a single drive for folders instead of files.

And again, although the illustration shows only a single folder mask,  like the previous match-spec demos the code actually allows for specifying multiple folders matching different folder specs using standard file wildcard symbols separated with a semicolon, i.e. "_vti*; *vbnet*; ccrp*" (all starting with '_vti', all containing 'vbnet', all starting with 'ccrp'). 

The routine presented also provides the ability to specify whether the output list should include the folders that match the specified folder mask, or whether the output should list only folders that do not contain any of the strings specified in the mask.

This demo shows output against a FrontPage \inetpub folder. FrontPage creates numerous folders beginning with the _vti extension that are used by FrontPage to maintain the site, and which, if deleted, can the site useless.  When Exclude is selected with the demo's _vti* mask, the routine returns all folders that do not begin with _vti.  

As the other multiple file spec routines have demonstrated, PathMatchSpec returns 1 if the passed file or folder name matches any entry in a passed mask, or 0 if no matches are found. Since the result is effectively a Boolean value, by comparing the API's return value to a desired value, we can have the MatchSpec function return True if the filename matches the option state (indicating "list the file"), or false if not.  This is the simple functionality behind the Included/Excluded listing above.

Notes:

  • The demo uses the VB6 FormatNumber function which is not available in VB4-32 or VB5. Users of versions prior to VB6 can use the API methods shown  without any problems, but should change all FormatNumber references to Format$ in the code below.
  • Any interface to display results (such as the listbox shown) will impact the performance of the routine. If you only need to locate files across a drive - no need to list the files as in this demo - the routine here is amazingly fast. Any action that requires constant (re)allocation of string resources will impede application performance, possibly seriously, as the string size grows. And I recommend avoiding *.* searches when adding the files to a list or other interface component.

  • This code was developed and run on Windows XP Pro. Under Win9x, where the number of entries in a list box is restricted, there is simply no way to list over 32k items.

 BAS Module Code
None.

 Form Code
Create a new project with a form containing five text boxes (Text1, Text2, Text3, Text4, Text5), a check box (Check1), a list box (List1), two option buttons in an option array (Option1(0), Option1(1), and a command button (Command1). Add labels as desired along with the following code.

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const vbDot = 46
Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackslash = "\"
Private Const ALL_FILES = "*.*"

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
   dwFileAttributes As Long
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As Long
   nFileSizeLow As Long
   dwReserved0 As Long
   dwReserved1 As Long
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type

Private Type FILE_PARAMS
   bRecurse As Boolean
   bFindOrExclude As Long  '1=find matching, 0=exclude matching
   nCount As Long
   nSearched As Long
   sFileNameExt As String
   sFolderExt As String
   sFileRoot As String
End Type

Private Declare Function FindClose Lib "kernel32" _
  (ByVal hFindFile As Long) As Long
   
Private Declare Function FindFirstFile Lib "kernel32" _
   Alias "FindFirstFileA" _
  (ByVal lpFileName As String, _
   lpFindFileData As WIN32_FIND_DATA) As Long
   
Private Declare Function FindNextFile Lib "kernel32" _
   Alias "FindNextFileA" _
  (ByVal hFindFile As Long, _
   lpFindFileData As WIN32_FIND_DATA) As Long

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Declare Function lstrlen Lib "kernel32" _
    Alias "lstrlenW" (ByVal lpString As Long) As Long

Private Declare Function PathMatchSpec Lib "shlwapi" _
   Alias "PathMatchSpecW" _
  (ByVal pszFileParam As Long, _
   ByVal pszSpec As Long) As Long

Private fp As FILE_PARAMS  'holds search parameters


Private Sub Form_Load()

   Text1.Text = "c:\inetpub"
   Text2.Text = "_vti*"
   Option1(0).Value = True
   Command1.Caption = "Begin Search"

End Sub


Private Sub Command1_Click()

   Dim tstart As Single   'timer var for this routine only
   Dim tend As Single     'timer var for this routine only
   
   Text3.Text = ""
   Text4.Text = ""
   Text5.Text = ""
   List1.Clear
   List1.Visible = False
   
   With fp
      .sFileRoot = QualifyPath(Text1.Text) 'start path
      .sFolderExt= Text2.Text              'folder type(s) of interest
      .bRecurse = Check1.Value = 1         'True = recursive search
      .nCount = 0                          'results
      .nSearched = 0                       'results
      .bFindOrExclude = IIf(Option1(0).Value = True, 1, 0) '0=include, 1=exclude
   End With
  
   tstart = GetTickCount()
   Call SearchForFolders(fp.sFileRoot)
   tend = GetTickCount()
   
   List1.Visible = True  
   Text3.Text = Format$(fp.nSearched, "###,###,###,##0")
   Text4.Text = Format$(fp.nCount, "###,###,###,##0")
   Text5.Text = FormatNumber((tend - tstart) / 1000, 2) & "  seconds"
                                    
End Sub


Private Sub SearchForFolders(sRoot As String)

   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
  
   hFile = FindFirstFile(sRoot & ALL_FILES, WFD)
  
   If hFile <> INVALID_HANDLE_VALUE Then
   
      Do
                  
         If (WFD.dwFileAttributes And vbDirectory) Then
            If Asc(WFD.cFileName) <> vbDot Then

             If fp.bRecurse Then
             
                 'check if the folder name matches the
                 'parameters specified for exclusion
                  If MatchSpec(WFD.cFileName, fp.sFolderExt) Then
                  
                     fp.nCount = fp.nCount + 1
                     
                    'list the full path to the folder
                     List1.AddItem sRoot & TrimNull(WFD.cFileName)
                     
                  End If
                  
                 'if a folder and recurse was specified,
                 'call method again passing the current folder
                  SearchForFolders sRoot & _
                                   TrimNull(WFD.cFileName) & _
                                   vbBackslash
                  
               End If
            End If
      
         End If 'If WFD.dwFileAttributes
      
         fp.nSearched = fp.nSearched + 1
      
      Loop While FindNextFile(hFile, WFD)
   
   End If 'If hFile
  
   Call FindClose(hFile)

End Sub


Private Function QualifyPath(sPath As String) As String

   If Right$(sPath, 1) <> vbBackslash Then
      QualifyPath = sPath & vbBackslash
   Else
      QualifyPath = sPath
   End If
      
End Function


Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlen(StrPtr(startstr)))
   
End Function


Private Function MatchSpec(sFile As String, sSpec As String) As Boolean

   MatchSpec = PathMatchSpec(StrPtr(sFile), StrPtr(sSpec)) = fp.bFindOrExclude
   
End Function
 Comments
Before running, assure that any hard-coded paths reflect accurate paths on your system.

It must also be noted that, since this example uses the listbox to return the results, on systems containing many folders you may, eventually, hit the listbox item limit of 32k items under an non-NT-based system. While there is no practical or reliable way to extend the number of items a listbox can contain on a Win9x system (without resorting to an owner-drawn control), you can increase the number of folders the method is capable storing  (if exceeding the size of a Long), by declaring the appropriate variables as Currency instead of Long.

Note: While it may be convenient to utilize VB's built-in constants in place of the FILE_ATTRIBUTE_* API values, care must be taken. There is a difference between related constant values that may cause unexpected performance at some point. For example, the constant 'vbNormal' is defined as having a value of 0, whereas the API FILE_ATTRIBUTE_NORMAL has a value of &H80 (decimal 128).


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter