+ Reply to Thread
Results 1 to 4 of 4

Getting list of files from folder and subfolder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Getting list of files from folder and subfolder

    So a new disaster today! I have the following code:

    Sub SomeSub()
    Call GetFiles("C:\Users\Me\SharePoint\Someone else\Shared_Folder") ', 0)
    End Sub
    
    Sub GetFiles(ByVal path As String)
    Dim FSO As Object
    Set FSO = CreateObject("scripting.filesystemobject")
    
    
    
    Dim folder As Object
    Set folder = FSO.GetFolder(path)
    
    Dim SubFolder As Object
    
    For Each SubFolder In folder.SubFolders
    
        Call GetFiles(SubFolder.path)
       
       
       
       Dim file As Object
    
        For Each file In folder.Files
        checkfile = file.Name
        
               If Right(checkfile, 3) = "xls" Then
                Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = file.Name
                Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = file.path
                Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = file.DateLastModified
               End If
               
        Next file
       
       
       
       
    Next SubFolder
    
    
    
    
    Set FSO = Nothing
    Set folder = Nothing
    Set SubFolder = Nothing
    Set file = Nothing
    
    End Sub
    And it actually works - well sort of. If I look at the results in some cases it gives the same file twice (even though there is only 1 in the folder). More worryingly though it seems to miss complete folders at the same level as the ones it returns. As always I feel like I'm doing something wrong. Am I or is this sort od code hit and miss (although it's been missing the same way all day long).

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Getting list of files from folder and subfolder

    This will create a a list of folders and files in columns A and B of the active sheet - you can list any other file attributes you need in other columns.....

    Dim FSO As Scripting.FileSystemObject
    Dim lngR As Long
    
    Sub ListFiles()
        FindFilesFromFolders "C:\Users\Me\SharePoint\Someone else\Shared_Folder"     'Change to desired parent folder
    End Sub
    
    Sub FindFilesFromFolders(strFolderName As String)
        Dim fsoFolder As Scripting.Folder
    
        If FSO Is Nothing Then
            Set FSO = New Scripting.FileSystemObject
        End If
        
        lngR = 1
        
        Set fsoFolder = FSO.GetFolder(strFolderName)
        FindFiles fsoFolder
        
    End Sub
    
    Sub FindFiles(fsoPFolder As Scripting.Folder)
        Dim fsoFile As Scripting.File
        Dim fsoSFolder As Scripting.Folder
        Dim wkbkW1 As Workbook
    
        For Each fsoFile In fsoPFolder.Files
            Cells(lngR, "A").Value = fsoPFolder.Path
            Cells(lngR, "B").Value = fsoFile.Name
            lngR = lngR + 1
        Next fsoFile
    
        For Each fsoSFolder In fsoPFolder.SubFolders
            FindFiles fsoSFolder
        Next fsoSFolder
    
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Getting list of files from folder and subfolder

    That seems to work - as in it at least has the first file I know was missing in the original list. CAn you tell me what the difference was?

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Getting list of files from folder and subfolder

    I think it was because you were processing subfolders first and launching a new FSO for each, which meant that your code would go to the deepest folder in the path, and find the files, then step out one level and reprocess it again - so however many folders you had, you would get that many multiples. I had 7 lisitngs for every file, because I had 7 sub folders in my path. But, I think it never fully processed the lowest level of sub-folder, so those files were missing - you needed to move your files loop outside your sub-folder loop:

    For Each SubFolder In folder.SubFolders
        Call GetFiles(SubFolder.path)
    Next SubFolder   'This is needed here
    
    For Each file In folder.Files
         'Code here
    Next file
    ' Next SubFolder   'And not here
    Last edited by Bernie Deitrick; 04-20-2017 at 01:37 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 14
    Last Post: 09-18-2016, 08:13 AM
  2. Replies: 2
    Last Post: 12-29-2015, 04:19 AM
  3. Replies: 0
    Last Post: 09-18-2015, 03:20 AM
  4. Replies: 12
    Last Post: 03-09-2015, 05:52 PM
  5. find list of files in different subfolder and move them to one folder
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-12-2015, 05:35 AM
  6. [SOLVED] Delete folder and subfolder OKB Files
    By laxmanann in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-11-2014, 02:05 PM
  7. Return files from a folder without Subfolder
    By EMoe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-18-2009, 11:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1