[英]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? 在目录“ C:/ searchfolder /”内的“ searchme.txt”中,文件夹中包含多个目录和文件-如何使其在该文件夹中搜索“ searchme.txt”并将结果返回到列表框?
Previously tried this to get the initial files, but returned 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 您需要声明fso
,它不会通过添加引用自动设置
Add this to the first line in the Sub 将此添加到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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.