2013-03-11 120 views
4

我寫了一個瀏覽所有路徑文件夾並搜索「strings.xml」文件的vba代碼。在VBA中搜索文件

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文件。

+0

您是否知道Office 2007的Office.FileSearch將不可用? – 2013-03-11 13:08:19

回答

0

我想你是說你要在子文件夾中查找「\值」爲所謂的文件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

菲利普

+0

那麼我正在尋找每個「values」文件夾中包含的每個「strings.xml」文件。我不應該這樣做.LookIn =「D:\ Workspace \ values」,因爲它只給出「D:\ Workspace \ values \ strings.xml」文件,但我也想要「D:\ Workspace \ .... \ values \ strings.xml「文件 – ARM 2013-03-11 13:15:42

+0

所以你需要做一個遞歸搜索的聲音...看看[如何:遞歸搜索目錄通過使用FileSystemObject](http://support.microsoft.com/kb/185601 ) – 2013-03-11 13:32:25

0

取代:

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 
1

下面將看recursiv在你的根目錄工作文件夾中找到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的工具►引用。