HOWTO: Recursively Search Directories Using FileSystemObject
ID: Q185601
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.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 Script Host
Windows NT Option Pack
Microsoft Internet Information Server 3.0
Scripting 3.1 upgrade
Visual Studio 98
Visual Basic 6.0
The FileSystemObject class gives better performance than using such Visual
Basic intrinsic functions as Dir and GetAttr, and is much simpler to
implement.
Example
- Start a new Standard EXE project in Visual Basic. Form1 is created by
default.
- 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 previously listed if
necessary.
- Add a CommandButton, a Label, and a ListBox to Form1. Adjust the width
of the Label to the width of the form.
- Paste the following code in the General Declarations section of Form1:
Sample Code
-----------
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
- 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:
Q185476
HOWTO: Search Directories to Find or List Files
Additional query words:
look subdirectory directory tree recursion recursive kbDSupport
kbDSD kbVBp500 File System Object filesystem kbVBp600
Keywords : kbGrpVB
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbhowto
|