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
Printable View
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
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.
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
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:
(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.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
Ah, this confused me at first, until I remembered that the Forgotten Realms uses the period as the thousands seperator. :pQuote:
Originally Posted by Asgorath
edit: too slow, was the same answer as the recurse one above...
@ 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
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.