在Excel VBA中迭代某个路径的所有文件?

在Excel VBA中迭代某个路径的所有文件?,excel,vba,file,filesystems,directory,Excel,Vba,File,Filesystems,Directory,我试图在特定驱动器中迭代一组.xlsx文件,以便任何文件都符合以下格式: H:\[*FOLDER NAME*]\SpecificFolder\Finalized Plans\2019 Prefix[*].xlsx 换句话说,在H驱动器上的文件夹中,子文件夹\SpecificFolder\Finalized plan\包含.xlsx文件,文件以文字短语“2019前缀”开头。比如说 H:\Widget\SpecificFolder\Finalized Plans\2019 Prefix Howdy

我试图在特定驱动器中迭代一组.xlsx文件,以便任何文件都符合以下格式:

H:\[*FOLDER NAME*]\SpecificFolder\Finalized Plans\2019 Prefix[*].xlsx
换句话说,在H驱动器上的文件夹中,子文件夹
\SpecificFolder\Finalized plan\
包含
.xlsx
文件,文件以文字短语
“2019前缀”
开头。比如说

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

我是否可以使用
Dir()
以某种方式对其进行迭代?还有别的办法吗


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

这里有一个非常快速的方法使用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
”改编自-->https://stackoverflow.com/a/31132876/4839827
公共子GetAllFileMatchingPattern(开始文件夹为字符串,FolderPattern为字符串)
如果右$(StartingFolder,1)“\”则StartingFolder=StartingFolder&“\”
将标准输出设置为字符串
将ws设置为工作表:设置ws=ThisWorkbook.Sheets(“Sheet1”)
变暗文件作为变量
StandardOutput=CreateObject(“WScript.Shell”).Exec(“CMD/C DIR”“”&StartingFolder&FolderPattern&“/S/B/A:-D”).StdOut.ReadAll
如果不是StandardOutput=vbNullString,则
文件=拆分(标准输出,vbCrLf)
ws.Range(“A1”).Resize(UBound(Files),1.Value2=Application.Transpose(Files)
如果结束
端接头
子示例()
GetAllFileMatchingPattern“H:\”,“*\SpecificFolder\Finalized Plans\2019前缀*.xlsx”
端接头

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

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
有关所有详细信息,请参阅下面的链接


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

@BigBen有多个这样的文件夹,可能包含“\SpecificFolder\Finalized Plans”等,这些文件夹名称之间不一定有任何相似之处,可以是任何名称。请尝试使用
文件系统对象
,特别是FolderExists方法。或者,尝试获取所有子文件夹中的所有文件,例如递归目录。然后,在获得完整列表后,应用所需文件的逻辑。@RyanWildry我不明白这是如何解决这个特定问题的?我不必提前知道文件夹名称以检查它们是否存在,我正在尝试遍历所有具有该模式的文件。请参阅我的其他注释,获取所有文件,然后应用您的模式。
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