DOCUMENT:Q185601 18-MAY-1998 [vbwin] TITLE :HOWTO: Recursively Search Directories Using FileSystemObject PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:5.0 OPER/SYS:WINDOWS KEYWORDS: ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0 --------------------------------------------------------------------- SUMMARY ======= The FileSystemObject class can be used to recursively search directories and find files. This article demonstrates the use of FileSystemObject to search for specific files. MORE INFORMATION ================ The FileSystemObject class is found in the Scrrun.dll file, which can be obtained by installing any one of the following packages: Windows Scripting Host Windows NT Option Pack IIS 3.0 Scripting 3.1 upgrade The FileSystemObject class gives better performance than using such Visual Basic intrinsic functions as Dir and GetAttr, and is much simpler to implement. Example ------- 1. Start a new Standard EXE project in Visual Basic. 2. On the Project menu, click References, and add a reference to the "Microsoft Scripting Runtime". If this option is not listed, browse for Scrrun.dll on your system. Install one of the tools listed above if necessary. 3. Add a Command Button, a Label, and a ListBox to Form1. Adjust the width of the Label to the width of the form. 4. Paste 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 Integer, nFiles As Integer, lSize As Long Dim sDir As String, sSrchString As String sDir = InputBox("Please enter the directory to search", _ "FileSystemObjects example", "C:\") sSrchString = InputBox("Please enter the file name to search", _ "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 Integer, nFiles As Integer) As Long Dim tFld As Folder, tFil As File, FileName As String 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 End Function 5. Run the project, and click Command1. Enter the directory and file name to search for. As each file is found, it is added to the list box. When the process is complete, the number of files found is displayed in a message box. The total size of the files is also shown. REFERENCES ========== For information on other methods you can use to find a specific file, please see the following article in the Microsoft Knowledge Base: ARTICLE-ID: Q185476 HOWTO: Search Directories to Find or List Files Additional query words: look subdirectory directory tree recursion recursive kbDSupport kbDSD kbVBp500 ====================================================================== Version : WINDOWS:5.0 Platform : WINDOWS Issue type : kbhowto ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1998.