Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / September 2003



Tip: Looking for answers? Try searching our database.

Retrieve a folder files count and size

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
YPC - 30 Sep 2003 22:09 GMT
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?

Thanks,
Yves..
Tom Esh - 30 Sep 2003 22:56 GMT
>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)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.