SizeOf Directory 
Author Message
 SizeOf Directory

How can I check the size of a directory, including all sub directories??


Fri, 23 Mar 2001 03:00:00 GMT  
 SizeOf Directory
I wrote a routine for an in-house app which works nicely. Here it is:

Public Function lBytesUsed(path As String) As Long
'returns total diskspace used by a path (subs included)
Dim dirs() As String, files() As String, temp As String, i As Integer
Dim numDirs As Integer, numFiles As Integer, totalBytes As Long

totalBytes = 0
temp = Dir(path, vbNormal + vbHidden + vbSystem + vbDirectory)

Do While temp <> ""
    If temp <> "." And temp <> ".." Then
        If bFileExists(path & temp) Then
            If (GetAttr(path & temp) And vbDirectory) = vbDirectory Then
                numDirs = numDirs + 1
                ReDim Preserve dirs(numDirs)
                dirs(numDirs) = temp
            Else
                numFiles = numFiles + 1
                ReDim Preserve files(numFiles)
                files(numFiles) = temp
                totalBytes = totalBytes + FileLen(path & temp)
            End If
        End If
    End If
    temp = Dir
Loop

If numDirs > 0 Then
    For i = 1 To numDirs
        totalBytes = totalBytes + lBytesUsed(path & dirs(i) & "\")
    Next i
End If
lBytesUsed = totalBytes
End Function

the function bFileExists uses an API function to determine that the file
exists (can't use Dir() because Dir is used earlier in the routine.. The
declaration for that call is:

Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal
lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
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

When using the routine, make sure you specify a path with a trailing '\', eg
"C:\DATA\"

Hope this helps.

Brian


Quote:
>How can I check the size of a directory, including all sub directories??



Fri, 23 Mar 2001 03:00:00 GMT  
 SizeOf Directory

Quote:
>How can I check the size of a directory, including all sub directories??

Here's one I prepared earlier (VB5 SP3).  Sample usage:

MsgBox DirSize("C:\") ' this does the root dir only
MsgBox DirSize("C:\WINDOWS\", IncludeSubDirs:=True) ' WINDOWS and all subdirectories

Note this misses hidden files, but it shouldn't be hard to change that if it's a problem
to you.  Also it was edited for posting so there might be typos.

'----------------------------------------------------------------------
Private Function DirSize(ByVal DirName As String, Optional ByVal IncludeSubDirs As _
Boolean) As Long
' For correct results, DirName should end in "\"
Dim FileName As Variant, Directories As New Collection, SubDirName as Variant

FileName = Dir(DirName)
While FileName <> ""
  DirSize = DirSize + FileLen(DirName & FileName)
  FileName = Dir
Wend

If IncludeSubDirs Then
  SubDirName = Dir(DirName, vbDirectory)
  While SubDirName <> ""
    If SubDirName <> "." And SubDirName <> ".." Then Directories.Add SubDirName
    SubDirName = Dir
  Wend
  For Each SubDirName In Directories
    DirSize = DirSize + DirSize(DirName & SubDirName & "\", IncludeSubDirs:=True)
  Next SubDirName
End If
End Function
'----------------------------------------------------------------------

HTH

--
-- Mike Barnes, Owner, Exodus Computer Systems, Stockport, England.
-- If you post a response to Usenet, please *don't* send me a copy by e-mail.



Sat, 24 Mar 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sizeof objects

2. VB6 SizeOf Method

3. SizeOf as in C

4. sizeof

5. Q: "Sizeof" in VB

6. sizeof in VB5?

7. Sizeof

8. Sizeof in vb?

9. sizeof for VB objects?

10. Memory allignment and sizeof() equivalent

11. Sizeof in VB

12. Q: sizeof operator

 

 
Powered by phpBB® Forum Software