|
-
Oct 10th, 2005, 09:41 AM
#1
Thread Starter
Lively Member
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
-
Oct 10th, 2005, 10:20 AM
#2
Frenzied Member
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.
-
Oct 10th, 2005, 10:22 AM
#3
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."
-
Oct 10th, 2005, 10:27 AM
#4
New Member
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:
Public Class ShowDirSize
Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long
Dim Size As Long = 0
' Add file sizes.
Dim fis As FileInfo() = d.GetFiles()
Dim fi As FileInfo
For Each fi In fis
Size += fi.Length
Next fi
' Add subdirectory sizes.
Dim dis As DirectoryInfo() = d.GetDirectories()
Dim di As DirectoryInfo
For Each di In dis
Size += DirSize(di)
Next di
Return Size
End Function 'DirSize
Public Overloads Shared Sub Main(ByVal args() As String)
If args.Length <> 1 Then
Console.WriteLine("You must provide a directory argument at the command line.")
Else
Dim d As New DirectoryInfo(args(0))
Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d))
End If
End Sub 'Main
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.
-
Oct 10th, 2005, 10:35 AM
#5
New Member
Re: Is there a way to get the size of a folder including subs
 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.
-
Oct 10th, 2005, 10:52 AM
#6
Hyperactive Member
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.
-
Jul 2nd, 2006, 07:08 AM
#7
Addicted Member
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
-
Jul 2nd, 2006, 09:06 AM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|