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(strFolderPat h)
Set subFldr = fldr.SubFolders
Set dbCurrent = CurrentDb
Set qryAddPathInfo = CurrentDb.QueryDefs("qryAd dDBPathSiz e")
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
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(strFolderPat
Set subFldr = fldr.SubFolders
Set dbCurrent = CurrentDb
Set qryAddPathInfo = CurrentDb.QueryDefs("qryAd
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER