VBA (Excel): Iterating through files in folder via FileSystemObject

前端 未结 3 1131
灰色年华
灰色年华 2020-12-06 08:53

Ok, so I consider myself an Excel VBA expert (even though I\'ve not done much with it for a while) but I\'m stumped on this one - that obviously means it is something extrem

3条回答
  •  我在风中等你
    2020-12-06 09:20

    I think you want to list all files in all folders and all sub-folders. Check out this link.

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

    Download the file; that's the way to go. Once all paths and all file names are listed in your Excel worksheet, you can do all kinds of comparisons, manipulations, and the like.

     Sub GetFilesInFolder(SourceFolderName As String)  
    
        '--- For Example:Folder Name= "D:\Folder Name\"  
    
        Dim FSO As Scripting.FileSystemObject  
        Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder  
        Dim FileItem As Scripting.File  
    
            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(""" & FileItem.Path & """,""" & "Click Here to Open" & """)"  
    
                r = r + 1   ' next row number  
            Next FileItem  
    
            Set FileItem = Nothing  
            Set SourceFolder = Nothing  
            Set FSO = Nothing  
        End Sub  
    
    
    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(""" & FileItem.Path & """,""" & "Click Here to Open" & """)"  
    
            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 
    

提交回复
热议问题