Ordering subdirectories by size -- please help!

A

almurph

Hi,

Can you help me please?

I know how to get a list of subdirectories under a directory as
follows:

string[] subDirs = Directory.GetDirectories(somePath);


but I do not know how to order them by size. I know there is an
OrderBy() method or something under this but I don't understand it. I
think it uses linq or something.

Can you help me please? Would greatly appreciate any comments/
suggestions/code-samples that you may be able to offer.

Thank you,
Al.
 
P

Peter Duniho

Hi,

Can you help me please?

I know how to get a list of subdirectories under a directory as
follows:

string[] subDirs = Directory.GetDirectories(somePath);


but I do not know how to order them by size. I know there is an
OrderBy() method or something under this but I don't understand it. I
think it uses linq or something.

You can't order the subdirectory names by size until you know what their
actual size is. Presumably, by "size" you mean the total size of all of
the items contained within the subdirectory, rather than the amount of
disk space the directory data itself takes up. To get that size, you need
to recursively traverse all of the contents within the subdirectory and
add up the sizes you find there.

Once you have that code in place, one possible approach would be to use a
SortedList<long, string> where the "long" key is the directory size and
the "string" value is the directory name. SortedList maintains a binary
tree structure for ordering, and once you've added all the subdirectories
to the collection using each's size as its key and the name of the
directory as the value, you can simply enumerate the collection to get the
elements in order.

Alternatively, you can create a simple helper struct that stores the size
and directory name together, building up a list of these as you enumerate
your directory names. Then once you've finished the enumeration, sort
that list of structs using the List<T>.Sort() method.

You could also use the Enumerable.OrderBy() extension method from LINQ on
any data structure in which you've stored the necessary information (e.g.
the above-mentioned List<T>).

In any case, you have to start with a data structure that has all the
information required to perform the sort in the first place. If you don't
know the sizes of the directories, there's no way to sort the directories
by size.

Pete
 
H

Harlan Messinger

Hi,

Can you help me please?

I know how to get a list of subdirectories under a directory as
follows:

string[] subDirs = Directory.GetDirectories(somePath);


but I do not know how to order them by size. I know there is an
OrderBy() method or something under this but I don't understand it. I
think it uses linq or something.

Can you help me please? Would greatly appreciate any comments/
suggestions/code-samples that you may be able to offer.

public static void SortDirectoriesBySize(string root)
{
string[] subDirs = Directory.GetDirectories(root);
var query =
from dir in subDirs
let files =
Directory.GetFiles(dir, "*.*",
SearchOption.AllDirectories)
let totalBytes =
(from file in files
select (new FileInfo(file)).Length).Sum()
orderby totalBytes
select new { Name = dir, Size = totalBytes };

foreach (var dir in query)
Console.WriteLine("{0} has {1} bytes", dir.Name, dir.Size);
}
 

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