GetSizeDirFile




Public Function CapPath(ByVal sPath As String, _
Optional ByVal sEnd As String, _
Optional ByVal sFile As String) As String

If (Not IsMissing(sEnd)) And (Right(sPath, Len(sEnd)) _
<> sEnd) Then sPath = sPath & sEnd
If Not IsMissing(sFile) Then sPath = sPath & sFile
CapPath = sPath

End Function

Public Function GetDirSize(ByVal sDirPath As String, _
Optional ByRef lFileCount As Long)

Dim sFile As String, sDir As String, sDirs() As String
Dim lDirSize As Long, lDirCount As Long

'Initialise dir size.

lDirSize = 0
lDirCount = 0

'Make sure the directory is capped.

sDirPath = CapPath(sDirPath, "\")

'Make array of all directories.

ReDim sDirs(0)
sDir = Dir(sDirPath, vbDirectory)

While Len(sDir) > 0
'Make sure that sDir is a directory and not .. or .

If (GetAttr(sDirPath & sDir) = vbDirectory) And _
(Left(sDir, 1) <> ".") Then
lDirCount = lDirCount + 1
'add room For Next element

ReDim Preserve sDirs(lDirCount)
'add Next element

sDirs(lDirCount) = CapPath(sDir, "\")
End If
'Get next directory.

sDir = Dir
Wend

'Retreives the first file.

sFile = Dir(sDirPath)

'Get all files in directory.

While Len(sFile) > 0
If GetAttr(sDirPath & sFile) <> vbDirectory Then
lDirSize = lDirSize + FileLen(sDirPath & sFile)
lFileCount = lFileCount + 1
Else
End If
'Get next file.

sFile = Dir
Wend
'Get subdirectories' info.

For lDirCount = 1 To (UBound(sDirs))
lDirSize = lDirSize + GetDirSize(sDirPath & _
sDirs(lDirCount), lFileCount)
Next lDirCount

GetDirSize = lDirSize

End Function
Inputs:
sDirectoryPath As String

Returns:
lDirSize As Long lFileCount As Long











( getsizedirfile.html )- by Paolo Puglisi - Modifica del 17/12/2023