Retrieving the Size of a Directory 
Author Message
 Retrieving the Size of a Directory

My current problem is retrieving the size of a directory.  So far it
looks like the only way to do this is to enumerate all of the files
and use GetFileSize on each one.  Does anyone know of an easier way?

Thanks,

Joe Turgeon

---
My e-mail address has been altered for discouraging
spammers.  To get my real address remove "-delete_this-"
from the posted address.



Tue, 12 Sep 2000 03:00:00 GMT  
 Retrieving the Size of a Directory

I got this off the newsgroup awhile back, I have not had a chance to try it
out so tell me how it goes if you give it a try.

Steve G.

Option Explicit
Public Const MAX_PATH = 255
Type FILETIME
     dwLowDateTime As Long
     dwHighDateTime As Long
End Type
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
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA"
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA"
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As
Long
Function lGetBytesInDirectory(sDir As String) As Long
    'Dim a long variable to hold a handle
    Dim lHandle As Long
    'Dim a structure for File Attributes
    Dim lpFindFileData As WIN32_FIND_DATA
    'Dim a string to hold the file name
    Dim sFileName As String
    'Dim a long variable to hold total bytes
    Dim lTotalBytes As Long
    lHandle = FindFirstFile(sDir & "\*.*", lpFindFileData)
    If lHandle Then
       Do
          sFileName = Left(lpFindFileData.cFileName,
InStr(lpFindFileData.cFileName, Chr$(0)) - 1)
          If sFileName <> "." And sFileName <> ".." Then
             If (GetAttr(sDir & "\" & sFileName) And vbDirectory) =
vbDirectory Then
                lTotalBytes = lTotalBytes + lGetBytesInDirectory(sDir &
"\" & sFileName)
             Else
                lTotalBytes = lTotalBytes + FileLen(sDir & "\" &
sFileName)
             End If
          End If
       Loop Until FindNextFile(lHandle, lpFindFileData) = 0
       CloseHandle lHandle
    End If
    lGetBytesInDirectory = lTotalBytes
End Function



Wed, 13 Sep 2000 03:00:00 GMT  
 Retrieving the Size of a Directory

On Sat, 28 Mar 1998 13:58:59 -0500, "Stephen Goneau"

Quote:

>I got this off the newsgroup awhile back, I have not had a chance to try it
>out so tell me how it goes if you give it a try.
>             Else
>                lTotalBytes = lTotalBytes + FileLen(sDir & "\" &
>sFileName)
>             End If

Interesting recursive routine, however I'm curious as to why it mucks
about with FileLen when the WIN32_FIND_DATA already contains the file
length as  nFileSizeLow (and nFileSizeHigh for really huge files).
Surely this code must be much slower as it has to access the file
twice.
Just a thought.
Regards.
Dave.


Wed, 13 Sep 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Retrieving Directory Size

2. Size file or size directory

3. retrieve directory (not file) info with an API (WinnNT4.0)

4. How to retrieve User Profile Directory

5. retrieving user profile directory on winnt

6. Retrieve Active Directory User Membership

7. Retrieve Active Directory User Membership

8. VB.Net - Retrieving a list of Organizational units from Active Directory

9. retrieving current directory

10. Retrieving just directories and browse Network Neighborhood

11. retrieving file list from directory?

12. VB - retrieving directories

 

 
Powered by phpBB® Forum Software