我需要搜索指定的文件,例如。 “searchme.txt”,在目录“C:/ searchfolder /”中,该文件夹中有多个目录和文件 - 如何让它搜索该文件夹中的“searchme.txt”并将结果返回到列表框? / p>
以前试过这个来获取初始文件,但没有返回结果:
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
答案 0 :(得分:1)
您需要声明fso
,它不会通过添加引用自动设置
将其添加到Sub
中的第一行Dim fso As New FileSystemObject
仅添加与文件名匹配的项目:
For Each fil In fld.Files
If fil.Name = "searchme.txt" Then
list1.AddItem fso.BuildPath(fld.Path, fil.Name)
End If
Next