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)
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