473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.

Dec 2 '06 #1
12 1730
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

<ga********@myway.comschreef in bericht
news:11**********************@j44g2000cwa.googlegr oups.com...
>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.

Dec 2 '06 #2
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.

Cor Ligthert [MVP] wrote:
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

<ga********@myway.comschreef in bericht
news:11**********************@j44g2000cwa.googlegr oups.com...
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.
Dec 2 '06 #3
Hi,

ga********@myway.com wrote:
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 2 '06 #4
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?

Dec 2 '06 #5
PS

<ga********@myway.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>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
>
any ideas for how I might do this?

Thankyou,

Gary.
Dec 2 '06 #6
Hi Gary,

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

size += FileSizes(d, searchpattern);

--
Dave Sexton

<ga********@myway.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
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?

Dec 2 '06 #7
Many Thanks Dave, that fixed it completely.

Dave Sexton wrote:
Hi Gary,

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

size += FileSizes(d, searchpattern);

--
Dave Sexton

<ga********@myway.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
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?
Dec 2 '06 #8
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:\TEMPgetdirs | 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]

<ga********@myway.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
|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.
|
Dec 3 '06 #9

William Stacey [C# MVP] wrote:
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:\TEMPgetdirs | 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.

Dec 4 '06 #10
but measure-object worked!

Cheers, nice cmdlet.

Hal

Dec 4 '06 #11
what is this? what is powershell?
Is there a likewise terse way of doing this in C# ?

Thanks,

Gary.
si******@yahoo.com wrote:
William Stacey [C# MVP] wrote:
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:\TEMPgetdirs | 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.
Dec 5 '06 #12
It is MSs new scripting language and shell. It is RTM so go download and
play.

--
William Stacey [C# MVP]

<ga********@myway.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
| what is this? what is powershell?
| Is there a likewise terse way of doing this in C# ?
|
| Thanks,
Dec 5 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Web Webon | last post by:
Hi everybody! I wonder if this is possible? I need to determine if a client is using "windows classic folders" or anything else. If I instantiate a Shell ActiveX object is there a way of...
1
by: Xiaopeng Qu | last post by:
Hi, I installed DB2 8.1 Express on the WindowsXP, and when I begin to use it, I found DB2 creates many new folders in C:\Documents and Settings. It creats more and more folders every day. All...
3
by: Rajiv Das | last post by:
VS 2003, XP SP2 ------------------------------------------------------------ DirectoryInfo temporary = new DirectoryInfo( Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));...
5
by: Gaetan | last post by:
I would like to guarantee that only one session at a time can request exclusive access to an object stored in Application. The ownership of the object must last throughout multiple HTTP requests. ...
0
by: bcanter | last post by:
I found a file on the web that will allow you to enumerate groups but it was an .hta and the top level admins won't allow this. I need to give managers access to the groups so that when a new user is...
2
by: Bruce Cleaver | last post by:
Hello, I've been trying to find a way to enumerate all drives- logical and virtual starting at the desktop for an explorer clone. I know how to get the logical drives and icons, but have not...
0
by: Bomac8 | last post by:
I have a text file with "|" as a delimeter with three (3) fields XXX-12345|\\YYYYYYY|\\ZZZZZZZZ I need to read these files into an output folder. The problem that I'm having is that I need to...
10
by: programmerboy | last post by:
As the subject says how can I check total # of files and folders in a particular folder and determine the total size of that folder. I will be using VB.NET. A small code snippet will be great. Thanks
0
by: samz | last post by:
Hello, Here is a simple PHP recursive file list (with interactive and visual FX) Sam's Files http://acc.jexiste.ch/JPN/RecurciveDIR12.RAR 1. How to ignore/avoid empty folders in this PHP script...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.