Results 1 to 7 of 7

Thread: Retieving size of folder and it's contents

  1. #1
    Guest
    Take a look at this thread. It will show how to list all subdirectories. I am almost sure you can list all files the way I showed using the code as well. I don't know about getting the Directories sizes though.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    'Put this in a classmodule
    Option Explicit
    Event Filecatch(File As String, Path As String, level As Integer)
    Event Direcatch(dire As String, Path As String, level As Integer)
    Public curlevel%
    Public filemax&
    Public diremax&
    Public tid&
    Sub explore(startdir$, Optional pauses = 100)
    Dim tids&
        tids = Timer
        filemax = 0: diremax = 0:  curlevel = 0
        If Right(startdir, 1) <> "\" Then startdir = startdir & "\"
        SubFiles startdir, pauses
    
        tid = Timer - tids
    End Sub
    
    
    
    Function SubFiles(Path$, Optional pauses = 100): Dim i&, dmax&, dirname$, dire$(), waiter&
        dirname = Dir(Path, 63)
        Do While dirname <> ""
            If dirname <> "." And dirname <> ".." Then
                If Int(GetAttr(Path + dirname) / 16) Mod 2 = 1 Then
                        If (dmax Mod 10) = 0 Then
                            ReDim Preserve dire(dmax + 10)    ' Resize the array.
                        End If
                    diremax = diremax + 1: dmax = dmax + 1
                    dire(dmax) = dirname
                    RaiseEvent Direcatch(dirname, Path, curlevel)
                Else
                    filemax = filemax + 1
                    RaiseEvent Filecatch(dirname, Path, curlevel)
                End If
            End If
                dirname = Dir   ' Get another directory name.
            waiter = waiter + 1: If waiter Mod (pauses) = 1 Then DoEvents
        Loop
        For i = 1 To dmax
            curlevel = curlevel + 1
            SubFiles Path & dire(i) & "\"
        Next i
        curlevel = curlevel - 1
    End Function
    'And this in a form 
    
    Private WithEvents sd As subdirs
    
    Public Dirsize As Long
    
    Private Sub sd_Filecatch(File As String, Path As String, level As Integer)
        Dirsize = Dirsize + FileLen(Path & "\" & File)
    End Sub
    
    Private Sub Form_Load()
        Set sd = New subdirs
        Dirsize = 0
        sd.explore "C:\Windows"
        MsgBox Dirsize
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Lively Member
    Join Date
    Jan 2000
    Location
    UK
    Posts
    66

    Thumbs down

    Hi! Thanks for your help on this but I keep getting:

    Compile Error:
    User-defined type not defined

    on the following line:

    Private WithEvents sd As subdirs

    Any ideas?

    Cheers

    Simon

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You used my class? then you should rename it to "subdirs".
    But i really recommend the API version, it's lot faster. But on the other hand if you want to have the files dropping in and speed is of no importance you could use my class
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Lively Member
    Join Date
    Jan 2000
    Location
    UK
    Posts
    66
    API? Where do I get that from

    Simon

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by Batman
    But, it will only work on normal subdirectories. If subdirectories have attributes of read-only, your program does not work. You need to change all lines that are like this.

    Code:
    If GetAttr(Temp & CurrentFolder) = vbDirectory Then
    'Change to this
    Code:
    If GetAttr(Temp & CurrentFolder) >= vbDirectory Then
    Or you could do like this:
    Code:
    If (GetAttr(Temp & CurrentFolder) And vbDirectory) = vbDirectory Then

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    As kedaman said:
    use the findfirst api to get first file in folder
    then use findnext to get next till you have them all
    in between all that as you get each file in the folder
    you use FileLen(thefile) to get it's size and just keep
    accumulating the totals

    You can get the API FindFirst and FindNext here:

    http://www.vbapi.com/ref/funca.html#f
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width