File Search - VB6
I need to search for a specified file, eg. "searchme.txt", within the directory "C:/searchfolder/", the folder has multiple directories and files within it - how can I make it search that folder for "searchme.txt" and return results to a list box?
Previously tried this to get the initial files, but returned开发者_Go百科 no results:
Private Sub SearchFolder(srcFol As String)
Dim fld As Folder, tFld As Folder, fil As File
Set fld = fso.GetFolder(srcFol)
If fld.Files.Count + fld.SubFolders.Count > 0 Then
For Each fil In fld.Files
list1.AddItem fso.BuildPath(fld.Path, fil.Name)
Next
For Each tFld In fld.SubFolders
If tFld.Files.Count + tFld.SubFolders.Count > 0 Then
SearchFolder tFld.Path
End If
DoEvents
If m_SearchRunning = False Then
Exit Sub
End If
Next
End If
End Sub
You need to declare fso
, it doesn't get set automatically by adding a reference
Add this to the first line in the Sub
Dim fso As New FileSystemObject
To only add the items that match the filename:
For Each fil In fld.Files
If fil.Name = "searchme.txt" Then
list1.AddItem fso.BuildPath(fld.Path, fil.Name)
End If
Next
精彩评论