List folders and sub-folders of a path with its size

i have seen couple of codes on the net for listing files from folders and sub folders, which does not really help me.

what i need is a VBA to run from Excel

prompts me to select a folder then once select it lists all folders and sub-folders from this path showing the followings information
Folder name,  Folder path , Folder Date created, Folder Size in Mega bites

Flora EdwardsMedicineAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Where do you want the "list" to be placed - in a text file, in an Excel sheet, etc?

No files are to be listed, just folders, correct?

Flora EdwardsMedicineAuthor Commented:
thanks Bill.

Yes,  files to be shown in Excel

No files,  just folders and its sub-folders
Bill PrewIT / Software Engineering ConsultantCommented:
Here's a basic starting point that will get you close.  Add this VBA code and run the first subroutine.  Make sure you add a reference to Microsoft Scripting Runtime.

Option Explicit

Sub Folders()
    Application.ScreenUpdating = False
    Dim strFolder As String
    Dim objSheet As Worksheet
    Dim objFolderPicker As Object
    Set objFolderPicker = Application.FileDialog(msoFileDialogFolderPicker)
    objFolderPicker.Title = "Choose the folder"
    If objFolderPicker.SelectedItems.Count <> 1 Then
        Exit Sub
    End If
    strFolder = objFolderPicker.SelectedItems(1) & "\"
    Set objSheet = ActiveWorkbook.Sheets.Add(, Worksheets(Worksheets.Count))
    objSheet.Cells(1, 1).Value = "Name"
    objSheet.Cells(1, 2).Value = "Path"
    objSheet.Cells(1, 3).Value = "Created"
    objSheet.Cells(1, 4).Value = "Size (MB)"
    ListFolders objSheet, strFolder
    Application.ScreenUpdating = True
End Sub

Sub ListFolders(objSheet As Worksheet, strFolder As String)
    Dim objFSO As Scripting.FileSystemObject
    Dim objFolder As Scripting.Folder
    Dim objSubFolder As Scripting.Folder
    Dim lngRow As Long
    Set objFSO = New Scripting.FileSystemObject
    Set objFolder = objFSO.GetFolder(strFolder)
    lngRow = objSheet.Cells(objSheet.Rows.Count, 1).End(xlUp).Row + 1
    objSheet.Cells(lngRow, 1).Value = objFolder.Name
    objSheet.Cells(lngRow, 2).Value = objFolder.Path
    objSheet.Cells(lngRow, 3).Value = objFolder.DateCreated
    objSheet.Cells(lngRow, 4).Value = objFolder.Size / 1024 / 1024
    For Each objSubFolder In objFolder.SubFolders
        ListFolders objSheet, objSubFolder.Path
    Next objSubFolder
    Set objSubFolder = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Flora EdwardsMedicineAuthor Commented:
many thanks Bill.

it worked.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.