VBA 中的文件搜索
Posted
技术标签:
【中文标题】VBA 中的文件搜索【英文标题】:File searching in VBA 【发布时间】:2013-02-26 15:42:18 【问题描述】:我写了一个 vba 代码,浏览所有路径文件夹并搜索“strings.xml”文件。
Dim oFS As Office.FileSearch
Dim i As Integer
Set oFS = Application.FileSearch
With oFS
.NewSearch
.FileType = msoFileTypeAllFiles
.Filename = "strings.xml"
.LookIn = "D:\Workspace"
.SearchSubFolders = True
.Execute
MsgBox "Finish ! " & .FoundFiles.Count & " item found !"
End With
但是,在我的工作区中,我有许多当前代码所在的“strings.xml”文件,但我只想在特定子文件夹中找到“strings.xml”;例如./values/strings.xml
文件。
【问题讨论】:
你知道从 Office 2007 开始,Office.FileSearch 将不可用吗? 【参考方案1】:以下内容将在您的根工作文件夹下递归查找Values\Strings.xml
匹配项,并将它们列在Scripting.Dictionary 对象中。
主要文件/文件夹搜索由简单的Dir function执行。
Sub dir_ValuesStringsXML_list()
Dim f As Long, ff As String, fp As String, fn As String, tmp As String
Dim vfn As Variant, dFILEs As Object 'New scripting_dictionary
Set dFILEs = CreateObject("Scripting.Dictionary")
dFILEs.CompareMode = vbTextCompare
'set vars for c:\temp\Workspace\*\Values\Strings.xml
fp = Environ("TMP") & Chr(92) & "Workspace"
ff = "Values"
fn = "Strings.xml"
dFILEs.Item(fp) = 0
'get folder list
Do
f = dFILEs.Count
For Each vfn In dFILEs
If Not CBool(dFILEs.Item(vfn)) Then
tmp = Dir(vfn & Chr(92) & Chr(42), vbDirectory)
Do While CBool(Len(tmp))
If Not CBool(InStr(1, tmp, Chr(46))) Then
dFILEs.Item(vfn & Chr(92) & tmp) = 0
End If
tmp = Dir
Loop
'Debug.Print dFILEs.Count
dFILEs.Item(vfn) = 1
End If
Next vfn
Loop Until f = dFILEs.Count
'remove the folders and check for Values\Strings.xml
For Each vfn In dFILEs
If CBool(dFILEs.Item(vfn)) Then
If LCase(Split(vfn, Chr(92))(UBound(Split(vfn, Chr(92))))) = LCase(ff) And _
CBool(Len(Dir(vfn & Chr(92) & fn, vbReadOnly + vbHidden + vbSystem))) Then
dFILEs.Item(vfn & Chr(92) & fn) = 0
End If
dFILEs.Remove vfn
End If
Next vfn
'list the files
For Each vfn In dFILEs
Debug.Print "from dict: " & vfn
Next vfn
dFILEs.RemoveAll: Set dFILEs = Nothing
End Sub
如果您希望将 Scripting.Dictionary 的后期绑定转换为早期绑定,您必须将 Microsoft Scripting Runtime 添加到 VBE 的工具 ► 参考中。
【讨论】:
【参考方案2】:我认为您是说要在子文件夹“\values”中查找名为 strings.xms
的文件如果正确,请尝试以下修改后的代码:
Dim oFS As Office.FileSearch
Dim i As Integer
Set oFS = Application.FileSearch
With oFS
.NewSearch
.FileType = msoFileTypeAllFiles
.Filename = "strings.xml"
.LookIn = "D:\Workspace\values"
.SearchSubFolders = True
.Execute
MsgBox "Finish ! " & .FoundFiles.Count & " item found !"
End With
当然,您可能不想指定子文件夹。
这是另一种选择:
Dim sPath As String
Dim sFil As String
Dim strName As String
sPath = "D:\Workspace\values" 'Change Path
sFil = Dir(sPath & "string.xml") 'All files in Directory matching name
Do While sFil <> ""
strName = sPath & sFil
sFil = Dir
'Your Code Here.
i=i+1
Loop
MsgBox "Finish ! " & .FoundFiles.Count & " item found !"
您是否考虑过使用 FileSystemObject 仅在子文件夹中进行递归搜索?
MSDN - How to do a recursive search using the FileSystemObject
HTH
菲利普
【讨论】:
嗯,我正在寻找每个“values”文件夹中包含的每个“strings.xml”文件。我不应该这样做 .LookIn = "D:\Workspace\values" 因为它只提供 "D:\Workspace\values\strings.xml" 文件,但我也想要 "D:\Workspace\....\values \strings.xml" 文件 所以你需要通过它的声音进行递归搜索......看看HOW TO: Recursively Search Directories by Using FileSystemObject【参考方案3】:替换:
sPath = "D:\Workspace\values" 'Change Path
sFil = Dir(sPath & "string.xml") 'All files in Directory matching name
与:
sPath = "D:\Workspace\values\" 'Change Path
sFil = Dir(sPath & "*.xl*") 'All files in Directory matching name
【讨论】:
以上是关于VBA 中的文件搜索的主要内容,如果未能解决你的问题,请参考以下文章