Knowledge Base

HOW TO: Recursively Search Directories by Using FileSystemObject

Article ID: 185601

Article Last Modified on 5/13/2003


APPLIES TO


This article was previously published under Q185601

IN THIS TASK

SUMMARY

This step-by-step article demonstrates how to use the FileSystemObject class to recursively search directories and to find specific files.

back to the top

Information About the FileSystemObject Class

The FileSystemObject class is located in the Microsoft Scripting Runtime (Scrrun.dll). To obtain the Scrrun.dll file, install any of the following packages: The FileSystemObject class provides better performance than if you use Visual Basic intrinsic functions such as Dir and GetAttr. Additionally, FileSystemObject is much simpler to implement that the Visual Basic intrinsic functions.

back to the top

Build the Sample

  1. Create a new Standard EXE project in Visual Basic. By default, Form1 is created.
  2. On the Project menu, click References, and then add a reference to the Microsoft Scripting Runtime. If this option is not listed, locate the Scrrun.dll file on your system. If you have to, install one of the tools that is listed in the Information About the FileSystemObject Class section.
  3. Add a CommandButton, a Label, and a ListBox control to Form1. Adjust the width of the Label control so that it is the same as the width of the form.
  4. Add the following code in the General Declarations section of Form1:
    Option Explicit
    
    Dim fso As New FileSystemObject
    Dim fld As Folder
    
    Private Sub Command1_Click()
       Dim nDirs As Long, nFiles As Long, lSize As Currency
       Dim sDir As String, sSrchString As String
       sDir = InputBox("Type the directory that you want to search for", _
                       "FileSystemObjects example", "C:\")
       sSrchString = InputBox("Type the file name that you want to search for", _
                       "FileSystemObjects example", "vb.ini")
       MousePointer = vbHourglass
       Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
       lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
       MousePointer = vbDefault
       MsgBox Str(nFiles) & " files found in" & Str(nDirs) & _
              " directories", vbInformation
       MsgBox "Total Size = " & lSize & " bytes"
    End Sub
    
    Private Function FindFile(ByVal sFol As String, sFile As String, _
       nDirs As Long, nFiles As Long) As Currency
       Dim tFld As Folder, tFil As File, FileName As String
       
       On Error GoTo Catch
       Set fld = fso.GetFolder(sFol)
       FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or _
                      vbHidden Or vbSystem Or vbReadOnly)
       While Len(FileName) <> 0
          FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, _
          FileName))
          nFiles = nFiles + 1
          List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
          FileName = Dir()  ' Get next file
          DoEvents
       Wend
       Label1 = "Searching " & vbCrLf & fld.Path & "..."
       nDirs = nDirs + 1
       If fld.SubFolders.Count > 0 Then
          For Each tFld In fld.SubFolders
             DoEvents
             FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
          Next
       End If
       Exit Function
    Catch:  FileName = ""
           Resume Next
    End Function
    					
  5. Run the project, and then click Command1.
  6. Type the directory and the file name to search for. Notice that when each file is found, the file name is added to the list box. When the process is complete, the number of files that are found is displayed in a message box. The total size of the files is also displayed.
back to the top

Troubleshooting

back to the top

REFERENCES

For additional information about other methods that you can use to find a specific file, click the article number below to view the article in the Microsoft Knowledge Base:

185476 HOWTO: Search Directories to Find or List Files

back to the top

Additional query words: look subdirectory directory tree recursion recursive File System Object filesystem

Keywords: kbhowto kbhowtomaster KB185601