【问题标题】:List files of certain pattern using Excel VBA使用 Excel VBA 列出特定模式的文件
【发布时间】:2010-10-08 00:47:45
【问题描述】:

如何列出用户指定目录中与特定模式匹配的所有文件?这应该在所选目录的子文件夹中递归地工作。我还需要一种方便的方式(如树形控件)来列出它们。

【问题讨论】:

标签: vba excel file-listing


【解决方案1】:

作为一般指针,请看一下 Application.FileSearch、递归函数、用户窗体和“Microsoft TreeView 控件”。

FileSearch 可用于在文件夹中查找与模式匹配的文件,递归函数可以调用自身直到所有路径都用尽,用户窗体可以托管用于显示数据的控件,而 TreeView 控件可以显示您的文件系统。

请记住,有一些预构建的函数/控件可用于显示文件系统,例如Application.GetOpenFileName、Application.GetSaveAsFileName、Microsoft WebBrowser(给定一个 'file://...' URL)。

【讨论】:

  • 是的,我没有要求完整的代码......我正在寻找这样的指针。谢谢...
  • 正确,但似乎 Application.FileSearch 自 Office 2007 起不再存在。您需要 Office 2003。
【解决方案2】:

尝试 Windows 脚本 - 文件系统对象。这个可以从 vba 创建的 COM 对象具有列出目录等功能。

您可以在 MSDN 上找到文档

【讨论】:

    【解决方案3】:

    不完全是你要求的,但我想我会在这里发布它,因为它是相关的。

    这是根据http://www.cpearson.com/excel/FOLDERTREEVIEW.ASPX 中的代码修改的

    这需要参考 Microsoft Scripting Runtime

    Sub ListFilePaths()
    
        Dim Path As String
        Dim Files As Long
    
        Path = "C:\Folder"
    
        Files = GetFilePaths(Path, "A", 1)
    
        MsgBox "Found " & Files - 1 & " Files"
    
    End Sub
    
    Function GetFilePaths(Path As String, Column As String, StartRow As Long) As Long
    
        Dim Folder As Scripting.Folder
        Dim SubFolder As Scripting.Folder
        Dim File As Scripting.File
        Dim FSO As Scripting.FileSystemObject
        Dim CurrentRow As Long
    
        Set FSO = New Scripting.FileSystemObject
        Set Folder = FSO.GetFolder(folderpath:=Path)
    
        CurrentRow = StartRow
    
        For Each File In Folder.Files
            Range(Column & CurrentRow).Value = File.Path
            CurrentRow = CurrentRow + 1
        Next File
    
        For Each SubFolder In Folder.SubFolders
            CurrentRow = GetFilePaths(SubFolder.Path, Column, CurrentRow)
        Next SubFolder
    
        GetFilePaths = CurrentRow
    
        Set Folder = Nothing
        Set FSO = Nothing
    End Function
    

    【讨论】:

      【解决方案4】:

      我看到我上面的人已经回答了如何通过文件树进行递归,这可能会让您对搜索文件/文件名中的模式感兴趣。这是一个允许使用正则表达式的 VBA 函数。

      Private Function RegularExpression(SearchString As String, Pattern As String) As String
      
          Dim RE As Object, REMatches As Object
      
          'Create the regex object' 
          Set RE = CreateObject("vbscript.regexp")
          With RE
              .MultiLine = False
              .Global = False
              .IgnoreCase = True
              'set the search pattern using parameter Pattern'
              .Pattern = Pattern 
          End With
      
          'Search for the pattern' 
          Set REMatches = RE.Execute(SearchString) 
          If REMatches.Count > 0 Then
              'return the first match'
              RegularExpression = REMatches(0) 
          Else
              'nothing found, return empty string'
              RegularExpression = ""
          End If
      
      End Function
      

      您可以使用它在文件名中搜索模式。我建议regular expressions home 了解更多关于如何使用正则表达式的信息

      【讨论】:

        【解决方案5】:

        似乎有几个答案是关于递归的,一个是关于正则表达式的。这是一些将两个主题放在一起的代码。我从http://vba-tutorial.com获取了代码

        Sub FindPatternMatchedFiles()
        
            Dim objFSO As Object
            Set objFSO = CreateObject("Scripting.FileSystemObject")
        
            Dim objRegExp As Object
            Set objRegExp = CreateObject("VBScript.RegExp")
            objRegExp.pattern = ".*xlsx"
            objRegExp.IgnoreCase = True
        
            Dim colFiles As Collection
            Set colFiles = New Collection
        
            RecursiveFileSearch "C:\Path\To\Your\Directory", objRegExp, colFiles, objFSO
        
            For Each f In colFiles
                Debug.Print (f)
                'Insert code here to do something with the matched files
            Next
        
            'Garbage Collection
            Set objFSO = Nothing
            Set objRegExp = Nothing
        
        End Sub
        
        Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
                        ByRef matchedFiles As Collection, ByRef objFSO As Object)
        
            Dim objFolder As Object
            Dim objFile As Object
            Dim objSubFolders As Object
        
            'Get the folder object associated with the target directory
            Set objFolder = objFSO.GetFolder(targetFolder)
        
            'Loop through the files current folder
            For Each objFile In objFolder.files
                If objRegExp.test(objFile) Then
                    matchedFiles.Add (objFile)
                End If
            Next
        
            'Loop through the each of the sub folders recursively
            Set objSubFolders = objFolder.Subfolders
            For Each objSubfolder In objSubFolders
                RecursiveFileSearch objSubfolder, objRegExp, matchedFiles, objFSO
            Next
        
            'Garbage Collection
            Set objFolder = Nothing
            Set objFile = Nothing
            Set objSubFolders = Nothing
        
        End Sub
        

        【讨论】:

        • 这太棒了!谢谢!无法抗拒自己复制这个:D
        • 那个脚本拯救了我的一天,尤其是正则表达式部分非常方便:D
        猜你喜欢
        • 2017-07-07
        • 2017-04-28
        • 1970-01-01
        • 2022-12-18
        • 2013-01-02
        • 2017-07-30
        • 1970-01-01
        • 2019-01-12
        • 2021-12-06
        相关资源
        最近更新 更多