Results 1 to 8 of 8

Thread: Is there a way to get the size of a folder including subs

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Location
    NH
    Posts
    90

    Is there a way to get the size of a folder including subs

    I am trying to get the size an entire subtree. I can't figure out how to do it with out enumerating thoiugh everything. Since though the OS you can just right click on the fold and get this I am guessing that it is possible...

    Thanks Scott

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Is there a way to get the size of a folder including subs

    I'm not aware of anyway to do it besides enumerating through everything. And if you look at how the OS does it, I would guess the OS is enumerating through all the files as well. It takes quite a while for a large folder and it increments itself up like it is counting.

  3. #3
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Is there a way to get the size of a folder including subs

    What windows do is adding the size of each files in the folder.
    I have a folder with 6 subfolders and 75.578 files and when i right click on it to get the properties it takes almost a minute to get total size.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  4. #4
    New Member
    Join Date
    Oct 2005
    Posts
    5

    Re: Is there a way to get the size of a folder including subs

    I'm thinking that it is not. If you observe the property form for a large folder with many files carefully, you'll see the size detail increasing as the folder is recursed. It's unfortunate, but I think you'll have to do something like this:
    VB Code:
    1. Public Class ShowDirSize
    2.  
    3.     Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long
    4.         Dim Size As Long = 0
    5.         ' Add file sizes.
    6.         Dim fis As FileInfo() = d.GetFiles()
    7.         Dim fi As FileInfo
    8.         For Each fi In fis
    9.             Size += fi.Length
    10.         Next fi
    11.         ' Add subdirectory sizes.
    12.         Dim dis As DirectoryInfo() = d.GetDirectories()
    13.         Dim di As DirectoryInfo
    14.         For Each di In dis
    15.             Size += DirSize(di)
    16.         Next di
    17.         Return Size
    18.     End Function 'DirSize
    19.  
    20.     Public Overloads Shared Sub Main(ByVal args() As String)
    21.         If args.Length <> 1 Then
    22.             Console.WriteLine("You must provide a directory argument at the command line.")
    23.         Else
    24.             Dim d As New DirectoryInfo(args(0))
    25.             Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d))
    26.         End If
    27.     End Sub 'Main
    28. End Class 'ShowDirSize
    (extracted verbatim from msdn). Before you use this code, I would double check that shortcuts to directories don't appear in the GetDirectories() collection, or the function could loop. I don't see why they would, but it never hurts.
    Last edited by segfault; Oct 10th, 2005 at 10:28 AM. Reason: Curses, foiled by slow posting.

  5. #5
    New Member
    Join Date
    Oct 2005
    Posts
    5

    Re: Is there a way to get the size of a folder including subs

    Quote Originally Posted by Asgorath
    I have a folder with 6 subfolders and 75.578 files
    Ah, this confused me at first, until I remembered that the Forgotten Realms uses the period as the thousands seperator.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Is there a way to get the size of a folder including subs

    edit: too slow, was the same answer as the recurse one above...
    Last edited by jo0ls; Oct 10th, 2005 at 10:56 AM.

  7. #7
    Addicted Member
    Join Date
    Dec 2005
    Location
    Germany, near Munich
    Posts
    172

    Re: Is there a way to get the size of a folder including subs

    @ segfault,

    thank you for this fine little class.

    Is it possible to write out the name of the actual subdirectory in a textbox, so that the progress is visible?

    Thanks & Greetz,

    TheTree
    ********* Look at http://www.bahai.org - and enjoy it! *********

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Is there a way to get the size of a folder including subs

    Of course. Just assign the FullName property of each DirectoryInfo object to the Text property of the TextBox, or probably a Label would be better. You'll probably have to call Refresh on the control in order to have it update. Note that this may noticably affect the speed of the calculation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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