【问题标题】:Iterate though all files of a certain path in Excel VBA?遍历Excel VBA中某个路径的所有文件?
【发布时间】:2019-06-03 13:13:04
【问题描述】:

我正在尝试遍历特定驱动器中的一堆 .xlsx 文件,以使任何文件都符合以下格式:

H:\[*FOLDER NAME*]\SpecificFolder\Finalized Plans\2019 Prefix[*].xlsx

换句话说,在 H 盘上的文件夹内,子文件夹 \SpecificFolder\Finalized Plans\ 包含以文字短语 "2019 Prefix" 开头的 .xlsx 文件。例如

H:\Widget\SpecificFolder\Finalized Plans\2019 Prefix Howdyado Neighbor.xlsx

H:\Rofl672\SpecificFolder\Finalized Plans\2019 Prefix Bob.xlsx

这是我可以使用Dir() 以某种方式迭代的东西吗?其他方式?

是不是类似于Dir("H:\*\SpecificFolder\Finalized Plans\2019 Prefix*.xlsx")? (这是我尝试过的,它给了我错误“错误的文件名或编号”,所以它可能不是正确的语法)

【问题讨论】:

  • @BigBen 有多个这样的文件夹可能包含 `\SpecificFolder\Finalized Plans` 等,这些文件夹名称之间不一定有任何相似之处,可以是任何东西。
  • 尝试使用FileSystemObject,特别是FolderExists 方法。 docs.microsoft.com/en-us/office/vba/language/reference/…。或者,尝试获取所有子文件夹中的所有文件,例如递归目录。然后在获得完整列表后应用您想要的文件的逻辑。
  • @RyanWildry 我不明白这是如何解决这个特定问题的?我不一定要提前知道文件夹名称来检查它们是否存在,我正在尝试遍历所有具有该模式的文件。
  • 查看我的其他评论,获取所有文件,然后应用您的模式。

标签: excel vba file filesystems directory


【解决方案1】:

这是一种使用 WScript 的非常快速的方法,它确实支持通配符。

'Adapted from --> https://stackoverflow.com/a/31132876/4839827
Public Sub GetAllFilesMatchingPattern(StartingFolder As String, FolderPattern As String)
    If Right$(StartingFolder, 1) <> "\" Then StartingFolder = StartingFolder & "\"
    Dim StandardOutput As String
    Dim ws             As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    Dim Files          As Variant
    StandardOutput = CreateObject("WScript.Shell").Exec("CMD /C DIR """ & StartingFolder & FolderPattern & """ /S /B /A:-D").StdOut.ReadAll

    If Not StandardOutput = vbNullString Then
        Files = Split(StandardOutput, vbCrLf)
        ws.Range("A1").Resize(UBound(Files), 1).Value2 = Application.Transpose(Files)
    End If
End Sub


Sub Example()
    GetAllFilesMatchingPattern "H:\", "*\SpecificFolder\Finalized Plans\2019 Prefix*.xlsx"
End Sub

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点。我想这会给你想要的。

    Sub GetFilesInFolder(SourceFolderName As String, Subfolders As Boolean)
    
    '--- For Example:Folder Name= "D:\Folder Name\" and Flag as Yes or No
    
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder
    Dim FileItem As Scripting.File
    'Dim r As Long
        Set FSO = New Scripting.FileSystemObject
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
    
        '--- This is for displaying, whereever you want can be configured
    
        r = 14
        For Each FileItem In SourceFolder.Files
            Cells(r, 2).Formula = r - 13
            Cells(r, 3).Formula = FileItem.Name
            Cells(r, 4).Formula = FileItem.Path
            Cells(r, 5).Formula = FileItem.Size
            Cells(r, 6).Formula = FileItem.Type
            Cells(r, 7).Formula = FileItem.DateLastModified
            Cells(r, 8).Formula = "=HYPERLINK(""" &amp; FileItem.Path &amp; """,""" &amp; "Click Here to Open" &amp; """)"
    
            r = r + 1   ' next row number
        Next FileItem
    
        '--- This is the Function to go each and Every Folder and get the Files. This is a Nested-Function Calling.
    
        If Subfolders = True Then
            For Each SubFolder In SourceFolder.Subfolders
                ListFilesInFolder SubFolder.Path, True
            Next SubFolder
        End If
    
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
    End Sub
    

    有关所有详细信息,请参阅下面的链接。

    http://learnexcelmacro.com/wp/2011/11/how-to-get-list-of-all-files-in-a-folder-and-sub-folders/

    单击标题为“立即下载”的链接以下载示例文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 2021-05-23
      相关资源
      最近更新 更多