>I am trying to retrieve the size and count of the files in a specific folder
>and subfolders. I need to do something like the Windows properties for a
>folder where the size, disk space, files and folders count. Is there any
>specific API that I can use to do this?
It takes several actually, but it's still ~much~ faster than using Dir
and FileLen. Here's a recursive routine I use that does it ('scuse the
line wraps):
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
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 * MAX_PATH
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
Private Declare Sub MemCopy Lib "kernel32" Alias "RtlMoveMemory"
(lpDest As Any, lpSource As Any, ByVal dwBytes As Long)
Public Function GetDirSizeCountEx(sPathSpec As String, ByRef
nFileCount As Long, ByRef nFolderCount As Long, Optional ByVal
InclSubDirs As Boolean = True) As Currency
'Returns total size of all files in a directory or path.
'Sets nFileCount to total number of files,
'nFolderCount to number of dirs.
'InclSubDirs setting:
' True (default if omitted) - Includes all subdirectories in
totals.
' False - Does not include subdirectories.
Dim FD As WIN32_FIND_DATA
Dim hFind As Long
Dim cSize As Currency, cSum As Currency, cSizeSubDir As Currency
Dim sPath As String, sSubDir As String
sPath = sPathSpec
If Right$(sPath, 1) <> "\" Then
sPath = sPath & "\"
End If
hFind = FindFirstFile(sPath & "*.*", FD)
If hFind <> INVALID_HANDLE_VALUE Then
Do
If FD.nFileSizeHigh <> 0& Then
MemCopy ByVal VarPtr(cSize) + 4, FD.nFileSizeHigh, 4
End If
MemCopy cSize, FD.nFileSizeLow, 4
cSum = cSum + cSize
If (FD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) =
FILE_ATTRIBUTE_DIRECTORY Then
sSubDir = Left$(FD.cFileName, InStr(1, FD.cFileName,
vbNullChar) - 1)
If (sSubDir <> ".") And (sSubDir <> "..") Then
If InclSubDirs Then 'recurse subdir
cSizeSubDir = cSizeSubDir +
GetDirSizeCountEx(sPath & sSubDir, nFileCount, nFolderCount, True)
End If
nFolderCount = nFolderCount + 1
End If
Else
nFileCount = nFileCount + 1
End If
Loop While FindNextFile(hFind, FD) <> 0&
FindClose hFind
End If
GetDirSizeCountEx = (cSum * 10000@) + cSizeSubDir
End Function
'==== ex usage ====
Dim Files As Long, Folders As Long
Dim TotSize As Currency
TotSize = GetDirSizeCountEx("c:\", Files, Folders)
Debug.Print "Files: " & Files
Debug.Print "Folders: " & Folders
Debug.Print "Total branch size: " & TotSize
-Tom
MVP - Visual Basic
(please post replies to the newsgroup)