listing files in subfolders

BBird

New Member
Joined
Oct 16, 2006
Messages
14
I am trying to list all based based on the last accessed date ans show file name, date created, modified, and accessed. As well as give a file count.

I have gotten the code to work for a specific folder, but cannot figure out how to modify it to list the files in any subfolders. The code I currently am using is below. Can someone kindly give me a hand please. (As a side note, I got this code from a post of GTO's in March of 2010)

Code:
Option Explicit
    
Sub filelist()
Dim rng As Range
'Dim RowNo As Integer
'Dim NoOfFiles As Integer
'Dim foundfilepath As Integer
Dim strFilePath As String
Dim FSO As Object
Dim fsoSourceFolder As Object
Dim fsoFileItem As Object
Dim r As Long ' Integer
Dim lFileMatchCount As Long
    
    '// For testing//
    'strFilePath = ThisWorkbook.Path & "\Temp\"
    strFilePath = "C:\test\"
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    If Not FSO.FolderExists(strFilePath) Then
        MsgBox "Error in Path", 0, vbNullString
        Exit Sub
    End If
    
    Set fsoSourceFolder = FSO.GetFolder(strFilePath)
    
    r = 2
    
    With Worksheets("FileChooser")
        For Each fsoFileItem In fsoSourceFolder.Files
            If fsoFileItem.DateLastModified < #1/1/2004# Then
                .Cells(r, 2).Formula = fsoFileItem.Name
                .Cells(r, 3).Formula = fsoFileItem.DateCreated
                .Cells(r, 4).Formula = fsoFileItem.DateLastModified
                .Cells(r, 5).Formula = fsoFileItem.DateLastAccessed
                r = r + 1
                lFileMatchCount = lFileMatchCount + 1
            End If
        Next fsoFileItem
        .Range("B1:E1").Value = Array("Name", "Created", "Modified", "Accessed")
        .Range("B1:E1").Font.Bold = True
        .Range("B:E").EntireColumn.AutoFit
        
        .Range("G1:H1").Value = Array("Matching Files", "Folder Files Count")
        .Range("G1:H1").Font.Bold = True
        .Range("G2:H2").Value = Array(lFileMatchCount, fsoSourceFolder.Files.Count)
        .Range("G1:H1").EntireColumn.AutoFit
End Sub
 
The name of the tab (worksheet) that you want the results to show in. Just for information, the spreadsheet is called a workbook and the individual tabs are called worksheets. In the code below, I needed to have the results show up on a worksheet called "FileChooser". If you do not have a worksheet with that name, you either need to change "FileChooser" to the name of a worksheet that fits your situation, or change the worksheet name in your workbook where you want the results to "FileChooser".
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top