【问题标题】:File Search - VB6文件搜索 - VB6
【发布时间】:2011-01-16 12:52:02
【问题描述】:

我需要搜索一个指定的文件,例如。 “searchme.txt”,在目录“C:/searchfolder/”中,该文件夹中有多个目录和文件 - 如何让它在该文件夹中搜索“searchme.txt”并将结果返回到列表框?

以前尝试过这个来获取初始文件,但没有返回任何结果:

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

【问题讨论】:

  • 到目前为止你尝试过什么?简单的谷歌搜索怎么样tinyurl.com/6ymfcw8
  • FileSystemObject,但是在测试时它没有返回任何结果(该文件存在于文件夹中)。
  • 请贴出你目前的代码..
  • fso 在哪里声明、设置?
  • fso 是参考:“Microsoft Scripting Runtime”

标签: file search vb6 directory


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多