Ideas for how to enumerate all folders on XP and determine size?

  • Thread starter Thread starter garyusenet
  • Start date Start date
G

garyusenet

I want to write a program that lists all folders on my local hard drive
in order of size,

any ideas for how I might do this?

Thankyou,

Gary.
 
Seems very simple to me,

Create your own class of file information with all the information you want
including size.

Fill that with the information from disk using the IO classes DirInfo and
FileInfo.
Sort that and show that.

Cor
 
Thankyou Cor, I'll look into DirInfo and Fileinfo now.

Would I be right in thinking I will need to store the lists of files
and folders into arraylists, instead of arrays?

Thankyou,

Gary.
 
Hi,

Thankyou Cor, I'll look into DirInfo and Fileinfo now.

Would I be right in thinking I will need to store the lists of files
and folders into arraylists, instead of arrays?

Thankyou,

Gary.

You won't need to store them yourself. Once you get a DirectoryInfo set
to the root folder, you can use the GetFiles and GetDirectories methods
which return arrays according to your query.

HTH,
Laurent
 
Hi as a stepping stone to what i'm trying to do i've decided to display
the size of all files within any given directory, including sub
directories. However it isn't working as expected, please find code
below and description of what's happening - any advice would be very
appreciated. TIA. Gary.

I'm using:
using System;
using System.IO;

namespace dirlist_cs
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// ListDirectory(new DirectoryInfo(@"c:\Program Files"));
// FullDirList(new DirectoryInfo(@"c:\program files\kru"),
"*.*");
string sizes = FileSizes(new DirectoryInfo(@"c:\George"),
"*.*").ToString();
Console.Write(sizes);
}

static long FileSizes(DirectoryInfo dir, string searchpattern)
{
long size = 0;
foreach (FileInfo f in dir.GetFiles(searchpattern))
{
size += (f.Length);
}
// process each directory
foreach (DirectoryInfo d in dir.GetDirectories())
{
FileSizes(d, searchpattern);
}

return size;
}

I had hoped that this would return the size of a given directory by
enumerating through the sizes of the files in the directory and it's
sub directories. But it's giving differing results.

In windows explorer, if I select the properties of a folder it is
displayed as:

Size: 4.01MB (4,213,751 bytes)
Size on disk: 4.07MB (4,276,224 bytes)

Yet console output from the above is: 2982849

Can someone explain to me what's going wrong please?
 
I want to write a program that lists all folders on my local hard drive
in order of size,

I assume you mean you want to list the folders by the size of the files
within the folder and not the size of the folder itself, correct?

PS
 
Hi Gary,

You forgot to add the size of the recursive result to the total:

size += FileSizes(d, searchpattern);
 
Many Thanks Dave, that fixed it completely.

Dave said:
Hi Gary,

You forgot to add the size of the recursive result to the total:

size += FileSizes(d, searchpattern);

--
Dave Sexton

Hi as a stepping stone to what i'm trying to do i've decided to display
the size of all files within any given directory, including sub
directories. However it isn't working as expected, please find code
below and description of what's happening - any advice would be very
appreciated. TIA. Gary.

I'm using:
using System;
using System.IO;

namespace dirlist_cs
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// ListDirectory(new DirectoryInfo(@"c:\Program Files"));
// FullDirList(new DirectoryInfo(@"c:\program files\kru"),
"*.*");
string sizes = FileSizes(new DirectoryInfo(@"c:\George"),
"*.*").ToString();
Console.Write(sizes);
}

static long FileSizes(DirectoryInfo dir, string searchpattern)
{
long size = 0;
foreach (FileInfo f in dir.GetFiles(searchpattern))
{
size += (f.Length);
}
// process each directory
foreach (DirectoryInfo d in dir.GetDirectories())
{
FileSizes(d, searchpattern);
}

return size;
}

I had hoped that this would return the size of a given directory by
enumerating through the sizes of the files in the directory and it's
sub directories. But it's giving differing results.

In windows explorer, if I select the properties of a folder it is
displayed as:

Size: 4.01MB (4,213,751 bytes)
Size on disk: 4.07MB (4,276,224 bytes)

Yet console output from the above is: 2982849

Can someone explain to me what's going wrong please?
 
Kinda OT, but here is a 1 liner in powershell (credit to Jacques Barathon of
MS):

$(gi .),$(dir -rec)|%{$_|?{$_.PSIsContainer}|select
fullname,@{n="Size";e={(dir $_.fullname|measure length -sum).sum}}}|sort
size -desc

Put it in a function or script and can do:

PS C:\TEMP> getdirs | ft -auto

FullName Size
-------- ----
C:\TEMP 10422377
C:\TEMP\pside 4503170
C:\TEMP\Console2 1642767
C:\TEMP\mort 21152
C:\TEMP\pside\Snippets\Crypt 2320
C:\TEMP\pside\Snippets\Files 1198
C:\TEMP\pside\Snippets\Software 965
C:\TEMP\pside\Snippets\Shares 787
C:\TEMP\pside\Snippets\Services 787
C:\TEMP\pside\Snippets 760

Kinda fun.
--
William Stacey [C# MVP]

|I want to write a program that lists all folders on my local hard drive
| in order of size,
|
| any ideas for how I might do this?
|
| Thankyou,
|
| Gary.
|
 
William said:
Kinda OT, but here is a 1 liner in powershell (credit to Jacques Barathon of
MS):

$(gi .),$(dir -rec)|%{$_|?{$_.PSIsContainer}|select
fullname,@{n="Size";e={(dir $_.fullname|measure length -sum).sum}}}|sort
size -desc

Put it in a function or script and can do:

PS C:\TEMP> getdirs | ft -auto

I get....

Select-Object : The term 'measure' is not recognized as a cmdlet,
function, operable program, or script file. Verify the term and try
again.
 
what is this? what is powershell?
Is there a likewise terse way of doing this in C# ?

Thanks,

Gary.
 
It is MSs new scripting language and shell. It is RTM so go download and
play.

--
William Stacey [C# MVP]

| what is this? what is powershell?
| Is there a likewise terse way of doing this in C# ?
|
| Thanks,
 

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

Back
Top