| Size of Directory | ||||||||||||||||
| New 23/11/2000 | ||||||||||||||||
| A few month ago, we had sort of a chanlange here at the office for what was supose to be the fastest way to get the size of a directory. Much like the windows does when you are getting the properties for the directory. I come up with two option one using the Dir function and another using the FindFirstFile API well all the test were very clear, if you are looking for speed use the API. | ||||||||||||||||
| Option Compare Database Option Explicit Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Private Const MAX_PATH = 260 Private Const FILE_ATTRIBUTE_DIRECTORY = &H10 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 * MAX_PATH cAlternate As String * 14 End Type Private Declare Function apiFindClose Lib "kernel32" _ Alias "FindClose" _ (ByVal hFindFile As Long) As Long Private Declare Function apiFindFirstFile Lib "kernel32" _ Alias "FindFirstFileA" _ (ByVal lpFileName As String, _ lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function apiFindNextFile Lib "kernel32" _ Alias "FindNextFileA" _ (ByVal hFindFile As Long, _ lpFindFileData As WIN32_FIND_DATA) As Long Public Function GetDirectorySize(ByVal strInitPath As String) As Double '----------------------------------------- 'Name: GetDirectorySize 'Purpose: Calculate the size of a Dir and all the subDir 'Version: 1.0 'Parameters strInitPath (String) 'Returns: The size in bytes of the Directory and SubDirectory 'Created by: Pedro Gil 'Feedback: pmpg98@hotmail.com 'Copyright: Pedro Gil 'Usage: GetDirectorySize("C:\Mydir") '----------------------------------------- Dim lpFindFileData As WIN32_FIND_DATA Dim hFile As Long Dim lpFileName As String Dim lngRet As Long Dim dblTotalSize As Double Dim strDirectoryName As String lpFileName = strInitPath + "\*.*" hFile = apiFindFirstFile(lpFileName, lpFindFileData) If hFile <= 0 Then Exit Function Do With lpFindFileData If (.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = _ FILE_ATTRIBUTE_DIRECTORY Then strDirectoryName = Left$(.cFileName, InStr(.cFileName, Chr$(0)) - 1) If strDirectoryName <> "." And strDirectoryName <> ".." Then dblTotalSize = dblTotalSize + GetDirectorySize(strInitPath + "\" + strDirectoryName) End If Else dblTotalSize = dblTotalSize + .nFileSizeLow End If End With lngRet = apiFindNextFile(hFile, lpFindFileData) Loop Until lngRet = 0 hFile = apiFindClose(hFile) GetDirectorySize = dblTotalSize End Function |
||||||||||||||||
| Any comment and problem's please send a e-mail to pmpg98@hotmail.com | ||||||||||||||||
| Home | Code Examples | |||||||||||||||