How to get folder size of a given folder (in two ways)
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 'File/Folder Handling - How to get folder size of a given folder (in two ways) 'Returns the size, in bytes, of the specified folder Option Explicit 'Using FSO: Public Function GetFolderSizeUsingFSO(ByVal sFolder As String) As Long 'Set a reference to "Microsoft Scripting Runtime" Dim FSO As Scripting.FileSystemObject Set FSO = New Scripting.FileSystemObject GetFolderSizeUsingFSO = FSO.GetFolder(sFolder).Size Set FSO = Nothing End Function 'How can I call this function: 'Debug.Print GetFolderSizeUsingFSO("C:\Temp\b\test1.txt") 'Using API: Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" _ (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" _ (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long Private Const FILE_ATTRIBUTE_DIRECTORY = &H10 Private Const MAX_PATH = 260 Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Public 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 * MAX_PATH cAlternate As String * 14 End Type Public Function GetDirectorySize(fld As String) As Currency Dim fHandle As Long Dim FileName As String Dim bRet As Boolean Dim findData As WIN32_FIND_DATA On Error Resume Next 'To Add a backslash if required Call SetPath(fld) 'To find the first file/folder in the root path fHandle = FindFirstFile(fld & "*", findData) 'strip null chars FileName = findData.cFileName FileName = StripNulls(FileName) 'loop through all files and folders Do While Len(FileName) <> 0 'if subfolder found then, drop into it If (findData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY And _ FileName <> "." And FileName <> ".." Then GetDirectorySize = GetDirectorySize + GetDirectorySize(fld & "\" & FileName) End If 'add the size to the total GetDirectorySize = GetDirectorySize + FileLen(fld & FileName) 'get the next one bRet = FindNextFile(fHandle, findData) 'No more file left in this folder so we can get out of this do loop If bRet = False Then Exit Do End If 'strip null chars FileName = findData.cFileName FileName = StripNulls(FileName) DoEvents Loop bRet = FindClose(fHandle) End Function Private Function SetPath(instring As String) As String 'To Add a back slash to a path if required If Right$(instring, 1) <> "\" Then instring = instring & "\" End If SetPath = instring End Function Private Function StripNulls(OriginalStr As String) As String 'Strip nulls from a string If (InStr(OriginalStr, chr ( 0 ) ) > 0) Then OriginalStr = Left$(OriginalStr, InStr(OriginalStr, chr ( 0 ) ) - 1) End If StripNulls = OriginalStr End Function 'How to use this function: 'cSize = GetDirectorySize(Foldername) 'This will return as currency format |