February 28, 1996
This article includes a Microsoft® Visual Basic® version 4.0 program that runs on the Microsoft Windows® 95 operating system. You can use this program to search a disk drive for a file or group of files. The search can be initiated from the root directory of the disk drive or from any specific directory. The found files are listed in a List Box control with their corresponding paths.
When you are running the Microsoft® Windows® 95 operating system, you can selectively search a disk for a specific file in a directory by using the Visual Basic® version 4.0 Dir function. However, when you want to find all occurrences of a specific file in a directory or when you want to find all matching wildcard filenames on a disk, you need to perform a recursive search routine.
The example program below lets you search a disk for a file or group of files matching the target filename. The target filename can be any valid MS-DOS® filename, including wildcard filenames. In addition, you can specify the directory from which you want to start the search process.
To find all occurrences of the target filename, a hidden List Box control is used to hold the names of all directories in which the search is to be performed. The Dir[$]() function is used in a Do-While loop to retrieve the names of all subdirectories under the target directory. As each directory name is found, its name is added to the hidden List Box control.
When you have retrieved the names of all directories stored under the target directory, you call the List_Files procedure. The List_Files procedure then performs another recursive search routine by using the same technique as the Get_Files procedure to find all files that match the target filename. The full path of the found file is added to the second List Box control. When all files and directories have been processed, the program quits.
This program shows how to search for a filename by specifying a directory from which to begin the search.
Dim XFilename As String
Dim StrtPath As String
Private Sub Get_Files(FPath As String)
Dim File_Name As String
Dim File_Path As String
Dim File_Read As Integer
Dim X As Boolean
Dim I As Integer
File_Path = FPath & "\"
File_Name = Dir$(File_Path, vbDirectory)
File_Read = 1
X = False
Do While File_Name <> ""
If File_Name <> "." And File_Name <> ".." Then
If GetAttr(File_Path & File_Name) = vbDirectory Then
StrtPath = File_Path & File_Name
List1.AddItem StrtPath
X = True
Get_Files StrtPath
End If
End If
If X = True Then
File_Name = Dir$(File_Path, vbDirectory)
For I = 2 To File_Read
File_Name = Dir$
Next
X = False
End If
File_Name = Dir$
File_Read = File_Read + 1
Loop
End Sub
Private Sub List_Files()
Dim XIndex As Integer
Dim XName As String
Dim X_Filename As String
For XIndex = 0 To List1.ListCount - 1
XName = List1.List(XIndex) & "\" & XFilename
X_Filename = Dir(XName)
If X_Filename <> "" Then
List2.AddItem List1.List(XIndex) & "\" & X_Filename
Do While True
X_Filename = Dir
On Error GoTo exit_loop
If X_Filename <> "" Then
List2.AddItem List1.List(XIndex) & "\" & X_Filename
Else
Exit Do
End If
Loop
End If
exit_loop:
Next XIndex
End Sub
Private Sub Command1_Click()
Dim XLen As Integer
XLen = Len(Text1.Text)
If Mid$(Text1.Text, XLen, 1) = "\" Then
StrtPath = Left$(Text1.Text, XLen - 1)
Else
StrtPath = Text1.Text
End If
XFilename = Text2.Text
List1.Clear
List2.Clear
List1.AddItem StrtPath
Get_Files StrtPath
List_Files
If List2.ListCount = 0 Then
MsgBox "Unable to find file"
Else
MsgBox "Search completed"
End If
End Sub
Run the example program by pressing f5. Suppose that you want to find all files with the extension .TXT in the directory C:\DOCS. In the first Text Box control, type the name of the directory from which you want to start the search. Next, type the name of the file you want to find in this starting directory. You may use any valid MS-DOS filename, as well as wildcard characters. Click the Search button. The program displays the results of its search in the List Box control and displays a message box indicating that the search has been completed.
Knowledge Base Q104685. "How to Retrieve Hidden/System Files Using Dir[$]() Function."
Knowledge Base Q119396. "How to Quickly List the Contents of a Directory."