Folder and File list count help

Eisasuarez

Well-known Member
Joined
Mar 23, 2012
Messages
653
Hi all - hope you are all good

Im hoping you can please help me with this. I believe this code lists all the file paths, name, subfolder files, size etc within the main folder

Can you please help me amend this code so that i can seperate and add the folder name?

I also write another sub that lists each folder and gives me a count of how many folders and files there are and also the count of subfolders there are and how many files there are

I guess i can use this code and need to amend it

Can you please help me - 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 = "File Path"
    Range("B1").Value = "File Name"
    Range("C1").Value = "File Size"
    Range("D1").Value = "Date Created"
    Range("E1").Value = "Date Last Accessed"
  
   
   '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
   
    '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 = objFile.Path
            Cells(NextRow, "B").Value = objFile.Name
            Cells(NextRow, "C").Value = objFile.Size / 1024
            Cells(NextRow, "D").Value = objFile.DateCreated
            Cells(NextRow, "E").Value = objFile.DateLastAccessed
            Cells(NextRow, "F").Value = objFile.DateLastModified
   
            NextRow = NextRow + 1
        End If
    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
 
[/Codw]
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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