Get directory contents size & down the subdirectory tree

P

Poster Matt

Hi Pros,

Is there a command to get the total size of a directory, all the files in
it, the size of all the files in its subdirectories, and so on down any
further levels of subdirectories? In other words given a directory path, the
total size of every file & subdirectory from there on down the directory tree?

Maybe I've just missed it but I can't seem to find a command to do that for
me in System.IO.

Do I really have to write recursive code to traverse through the whole tree
to get the total size, when you can get that from the Window's OS with a
couple of mouse clicks (right-Click directory name, click on 'properties'
and it'll tell you).

Thanks.
 
P

Peter Duniho

[...]
Do I really have to write recursive code to traverse through the whole
tree to get the total size, when you can get that from the Window's OS
with a couple of mouse clicks (right-Click directory name, click on
'properties' and it'll tell you).

When you perform that operation in Windows, it has to do exactly what
you're describing. It can cache that information so that it can be
displayed more quickly if you look at it again shortly after checking it,
but otherwise it's the same approach (and your program can cache it too).

Pete
 
T

Tom Spink

Poster said:
Hi Pros,

Is there a command to get the total size of a directory, all the files in
it, the size of all the files in its subdirectories, and so on down any
further levels of subdirectories? In other words given a directory path, the
total size of every file & subdirectory from there on down the directory tree?

Maybe I've just missed it but I can't seem to find a command to do that for
me in System.IO.

Do I really have to write recursive code to traverse through the whole tree
to get the total size, when you can get that from the Window's OS with a
couple of mouse clicks (right-Click directory name, click on 'properties'
and it'll tell you).

Thanks.

I don't know about Windows - because I don't have it, so I can't test
it. But, in my experience, there's no other way to do it than recursive
searching.

Maybe Windows maintains some kind of index, with file metadata cached -
I don't know, but that's certainly what the Linux tools do!
 
P

Poster Matt

Peter said:
[...]
Do I really have to write recursive code to traverse through the whole
tree to get the total size, when you can get that from the Window's OS
with a couple of mouse clicks (right-Click directory name, click on
'properties' and it'll tell you).

When you perform that operation in Windows, it has to do exactly what
you're describing. It can cache that information so that it can be
displayed more quickly if you look at it again shortly after checking
it, but otherwise it's the same approach (and your program can cache it
too).

Ok thanks Peter, I just assumed .net would have a method to do this is one
of the many System.IO classes. Anyway the code only took a few minutes and
it's done and working fine.

If anyone is searching for the same thing and finds this post, here's the
code (be careful if you ran it with c:\ it might take a long time):

long GetTotalSizeOfDirectoryTree(string dir)
{
long total = 0;

string[] files = Directory.GetFiles(dir);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
long fileLength = fi.Length;
total += fileLength;
}

string[] subDirectoryEntries = Directory.GetDirectories(dir);
foreach (string subDirectory in subDirectoryEntries)
{
total += TestGetSize(subDirectory); // Recursive call
}

return total;
}
 
P

Poster Matt

Tom said:
I don't know about Windows - because I don't have it, so I can't test
it. But, in my experience, there's no other way to do it than recursive
searching.

Maybe Windows maintains some kind of index, with file metadata cached -
I don't know, but that's certainly what the Linux tools do!

Thanks.
 
P

Poster Matt

Oops, the code in the previous post had an error. I changed the method name
before posting to something more descriptive (it was 'TestGetSize') and then
forgot to change the recursive call method name. It should have been this:

long GetTotalSizeOfDirectoryTree(string dir)
{
long total = 0;

string[] files = Directory.GetFiles(dir);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
long fileLength = fi.Length;
total += fileLength;
}

string[] subDirectoryEntries = Directory.GetDirectories(dir);
foreach (string subDirectory in subDirectoryEntries)
{
// Recursive call
total += GetTotalSizeOfDirectoryTree(subDirectory);
}

return total;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top