VBA中的文件搜索

VBA中的文件搜索,vba,excel,dir,Vba,Excel,Dir,我编写了一个vba代码,可以浏览所有路径文件夹并搜索“strings.xml”文件 然而,在我的工作区中,我有许多当前代码所定位的“strings.xml”文件,但我只想在特定的子文件夹中找到“strings.xml”;e、 g./values/strings.xml文件。我想你是说你想在子文件夹“\values”中查找名为strings.xms的文件 如果正确,请尝试以下修订代码: Dim oFS As Office.FileSearch Dim i As Integer Set oFS =

我编写了一个vba代码,可以浏览所有路径文件夹并搜索“strings.xml”文件


然而,在我的工作区中,我有许多当前代码所定位的“strings.xml”文件,但我只想在特定的子文件夹中找到“strings.xml”;e、 g.
/values/strings.xml
文件。

我想你是说你想在子文件夹“\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 !"
Dim sPath作为字符串
将sFil设置为字符串
将strName设置为字符串
sPath=“D:\Workspace\values”'更改路径
sFil=Dir(sPath&“string.xml”)'目录中与名称匹配的所有文件
当sFil“”时执行
strName=sPath&sFil
sFil=Dir
'您的代码在这里。
i=i+1
环
MsgBox“Finish!”&.FoundFiles.Count&“找到项!”
您是否考虑过只在子文件夹中使用FileSystemObject进行递归搜索

菲利普

替换:

sPath = "D:\Workspace\values" 'Change Path
sFil = Dir(sPath & "string.xml") 'All files in Directory matching name
与:


下面将在根工作文件夹下递归查找
Values\Strings.xml
匹配项,并在对象中列出它们

主文件/文件夹搜索由simple执行



如果希望将Scripting.Dictionary的后期绑定转换为早期绑定,则必须将添加到VBE的工具中► 参考资料。

您知道Office 2007的Office.FileSearch不可用吗?我正在查找每个“values”文件夹中包含的每个“strings.xml”文件。我不应该这样做。LookIn=“D:\Workspace\values”,因为它只提供了“D:\Workspace\values\strings.xml”文件,但我还需要“D:\Workspace\..\values\strings.xml”文件,所以您需要根据它的声音进行递归搜索。。。看看
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
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