Hi - Im trying to list out the names of all folders and files in a folder and also get the size, name, modified etc
I also am excluding any hidden files as this appears to be included in my count. Im trying to get a count of all the sub folders and count of all files also
This code appears to work for me but has been running for an hour or so and then my system crashed as there is that many subfolders and files in the root folder
Please help me speed this up
Thank You
I also am excluding any hidden files as this appears to be included in my count. Im trying to get a count of all the sub folders and count of all files also
This code appears to work for me but has been running for an hour or so and then my system crashed as there is that many subfolders and files in the root folder
Please help me speed this up
Thank You
Code:
Sub ListFiles()
'Set a reference to Microsoft Scripting Runtime by using
'Tools > References in the Visual Basic Editor (Alt+F11)
'Declare the variables
Dim objFSO As Scripting.FileSystemObject
Dim objTopFolder As Scripting.Folder
Dim strTopFolderName As String
Application.ScreenUpdating = False
'Assign the top folder to a variable
FolderPath = "S:\CR\Resource Planning\"
'With Sheet1
'Insert the headers for Columns A through F
Range("A1").Value = "Folder Path"
Range("B1").Value = "Folder Name"
Range("C1").Value = "File Name"
Range("D1").Value = "File Size"
Range("E1").Value = "Date Created"
Range("F1").Value = "Date Last Accessed"
Range("G1").Value = "Date Last Modified"
Range("H1").Value = "Count of Subfolders"
Range("I1").Value = "Count of Files"
'End With
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the top folder
Set objTopFolder = objFSO.GetFolder(FolderPath)
'Call the RecursiveFolder routine
Call RecursiveFolder(objTopFolder, True)
'Change the width of the columns to achieve the best fit
Columns.AutoFit
Application.ScreenUpdating = True
End Sub
Sub RecursiveFolder(objFolder As Scripting.Folder, _
IncludeSubFolders As Boolean)
'Declare the variables
Dim objFile As Scripting.File
Dim objSubFolder As Scripting.Folder
Dim NextRow As Long
Dim fileCount As Long
'Find the next available row
NextRow = Cells(Rows.Count, "C").End(xlUp).Row + 1
'Loop through each file in the folder
For Each objFile In objFolder.Files
If Not objFile.Attributes And 2 Then
Cells(NextRow, "A").Value = objFolder.Path
Cells(NextRow, "B").Value = objFolder.Name
Cells(NextRow, "C").Value = objFile.Name
Cells(NextRow, "D").Value = Round(objFile.Size / 1024, 2)
Cells(NextRow, "E").Value = objFile.DateCreated
Cells(NextRow, "F").Value = objFile.DateLastAccessed
Cells(NextRow, "G").Value = objFile.DateLastModified
For Each File In objFolder.Files
If (File.Attributes And vbHidden) = 0 Then fileCount = fileCount + 1
If Cells(NextRow, "B") <> Cells(NextRow - 1, "B") Then
Cells(NextRow, "H").Value = objFolder.subfolders.Count
Cells(NextRow, "I").Value = fileCount
Else
Cells(NextRow, "H").Value = 0
Cells(NextRow, "I").Value = 0
End If
Next File
NextRow = NextRow + 1
End If
fileCount = 0
Next objFile
'Loop through files in the subfolders
If IncludeSubFolders Then
For Each objSubFolder In objFolder.subfolders
Call RecursiveFolder(objSubFolder, True)
Next objSubFolder
End If
End Sub