PDA

Click to See Complete Forum and Search --> : Size


benchin
Jun 11th, 2001, 02:29 AM
Is there any API that can get the size of directory? I found GetFileSize. Does it applies for directory? Any example? Thanks!;)

Vlatko
Jun 11th, 2001, 05:28 AM
You can loop through the files in the directory and get the size of each file.

Nucleus
Jun 11th, 2001, 05:36 AM
Option Explicit

Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const MAXDWORD = &HFFFF
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Dim bytes As Double

Public Function NuFolderSize(ByVal Path As String, Optional IncludeSubdirectories = False) As Double
'Nucleus
NuFolderSize = TotalBytes(Path, IncludeSubdirectories)
bytes = 0
End Function

Private Function TotalBytes(ByVal Path As String, Optional IncludeSubdirectories = False) As Double
Dim hfind&, fname$
Dim WFD As WIN32_FIND_DATA
Dim found As Boolean

If Right(Path, 1) <> "\" Then Path = Path & "\"
If Len(Dir$(Path, vbDirectory)) <> 0 Then
hfind = FindFirstFile(Path & "*", WFD)
found = (hfind > 0)
While found
fname = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr$(0)) - 1)
If fname <> "." And fname <> ".." Then
If WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
If IncludeSubdirectories Then TotalBytes Path & fname, IncludeSubdirectories
Else
bytes = bytes + WFD.nFileSizeHigh * MAXDWORD + WFD.nFileSizeLow
End If
End If
found = FindNextFile(hfind, WFD)
Wend
End If
FindClose hfind
TotalBytes = bytes
End Function
Private Sub Command1_Click()
'comparison between fso And nuFolderSize
'Need To Set a reference To microsoft scripting runtime
Dim FSO As Scripting.FileSystemObject
Dim folder As Scripting.folder
Dim dr As Scripting.Drive
Set FSO = New Scripting.FileSystemObject
Set folder = FSO.GetFolder("d:\")

'FSO folder size
MsgBox Format(folder.Size / 1024, "#,#") & "KB"

'NuFolderSize
MsgBox Format(NuFolderSize("d:\", True) / 1024, "#,#") & "KB"

'FSO TotalDriveSize
Set dr = FSO.GetDrive("d")
MsgBox Format(dr.TotalSize / 1024, "#,#") & "KB"
Set FSO = Nothing
End Sub