File searching in VBA

后端 未结 3 968
青春惊慌失措
青春惊慌失措 2020-12-21 01:29

I wrote a vba code that browse all path folder and search for \"strings.xml\" file.

Dim oFS As Office.FileSearch
Dim i As Integer
Set oFS = Application.FileS         


        
3条回答
  •  眼角桃花
    2020-12-21 01:58

    The following will look recursively under your root working folder for Values\Strings.xml matches and list them in a Scripting.Dictionary object.

    The main file/folder search is performed by the simple 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
    

    If you wish to convert the late binding of the Scripting.Dictionary to early binding, you must add Microsoft Scripting Runtime to the VBE's Tools ► References.

提交回复
热议问题