Link to home
Start Free TrialLog in
Avatar of msanzenb
msanzenb

asked on

Get name and size for directory and all subdirectories

I need to get directory name and size for a directory and all of its subdirectories (some are 2, 3 and 4 levels down).  The code I'm currently using is below, but this will only go down one level.

Dim fso As Scripting.FileSystemObject
Dim fldr
Dim subFldr
Dim lngSize As Double
Dim s
Dim strPathName As String
Dim strPath As String
Dim dbCurrent As DAO.Database
Dim qryAddPathInfo As DAO.QueryDef

If (Dir$(strFolderPath, vbDirectory) = "") Then Exit Sub
Set fso = New Scripting.FileSystemObject
Set fldr = fso.GetFolder(strFolderPath)
Set subFldr = fldr.SubFolders

Set dbCurrent = CurrentDb
Set qryAddPathInfo = CurrentDb.QueryDefs("qryAddDBPathSize")

For Each s In subFldr
    lngSize = s.Size
    'Convert folder size to gigabytes (from bytes)
    lngSize = (((lngSize / 1024) / 1024) / 1024)
    strPath = s.Path
   
    With qryAddPathInfo
        .Parameters(Path) = strPath
        .Parameters("Size") = lngSize
        .Execute
    End With
Next

Set dbCurrent = Nothing
Set qryAddPathInfo = Nothing
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msanzenb
msanzenb

ASKER

Thanks - that worked perfect!!