Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
' lists information about the files in SourceFolder
'ListFilesInFolder "g:\data\", True
'NOTE: need Microsoft Scripting Runtime (scrrun.dll) from Tools, Reference
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)
Cells(1, 1).Formula = "Path"
Cells(1, 2).Formula = "File Name"
Cells(1, 3).Formula = "File Size"
Cells(1, 4).Formula = "File Type"
Cells(1, 5).Formula = "Date Created"
Cells(1, 6).Formula = "Date Last Accessed"
Cells(1, 7).Formula = "Date Last Modified"
Cells(1, 8).Formula = "Attributes"
r = Cells(Rows.Count, "A").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
' display file properties
Cells(r, 1).Formula = FileItem.Path
Cells(r, 2).Formula = FileItem.Name
Cells(r, 3).Formula = FileItem.Size
Cells(r, 4).Formula = FileItem.Type
Cells(r, 5).Formula = FileItem.DateCreated
Cells(r, 6).Formula = FileItem.DateLastAccessed
Cells(r, 7).Formula = FileItem.DateLastModified
Cells(r, 8).Formula = FileItem.Attributes
' next row number
r = r + 1
Next FileItem
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
Columns("A:H").AutoFit
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
ActiveWorkbook.Saved = True
End Sub