开发者

File Search - VB6

开发者 https://www.devze.com 2023-02-04 19:04 出处:网络
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 f

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
0

精彩评论

暂无评论...
验证码 换一张
取 消