473,404 Members | 2,114 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,404 software developers and data experts.

Directory size on disk

12
Hello,
I wish to find out the total space (on disk) of a given directory \ folder on windows platform.
Does anybody know how to do it ?

Thanks,
Ido.
Aug 1 '07 #1
17 16987
numberwhun
3,509 Expert Mod 2GB
What have you tried so far? Please post the code you have used to solve this and we can assist from there.

Just a tip though, you may want to search CPAN for modules to do what you want. If you go to search.cpan.org and search for "size", you will see an entry for File::Size which will return the size of files and directories.

Regards,

Jeff
Aug 1 '07 #2
idoha
12
Hello Jeff,
Thanks for your reply.

I used stat function which returns several parameters regarding a given file path, one of which holds the file size. But unfortunately this value is only the actual size of file, not the space it occupies on the disk. I need "size on disk" value.

I have been trying to use WMI as well, but it doesn't contain any "size" values on directories, just on files (and this is the actual size as well).

One more thing, I am trying to avoid a case of manually drill down into the folder and accumulate all its sub directory's files size (presuming I will be able to find out their "size on disk" values).

Thanks anyway,
Ido.
Aug 1 '07 #3
numberwhun
3,509 Expert Mod 2GB
Ok, have you tried the File::Size module that I mentioned in my last post? Also, for dealing with file size, try the File::Util module as it returns the file size in bytes, which should be size on disk as you need.

It just takes a bit of searching on CPAN. You want to deal with files? Search for "file" or "size" and see what comes up. A brief description of what each module does IS given for your review.

Regards,

Jeff
Aug 1 '07 #4
KevinADC
4,059 Expert 2GB
File::Size is an unauthorized release, which I am not sure what that means, but it may still work OK, I have never tried using it.

http://search.cpan.org/~ofer/File-Size-0.06/
Aug 1 '07 #5
KevinADC
4,059 Expert 2GB
personally, I might do it like this:

Expand|Select|Wrap|Line Numbers
  1. chdir('c:/');
  2. my @dirinfo = qx|dir c:\windows /S|;
  3. print @dirinfo[-2,-1];
and just parse out the size info.

Note: in the qx|| string I had to use a backslash in front of windows: c:\windows. While Windows allows you to use forward or backslashes in directory paths, DOS does not. Forward slashes are option switches in DOS commands.

As far as finding out the actual disk blocks a file is using, versus it's file size, I do not know how to do that. You may have to use a third party application that can figure that out.
Aug 1 '07 #6
idoha
12
Hi Again,

I have got 1 major constraint: I cannot use an external package such as File::size since I cannot demand from the customer I am doing it for to install that package either. Therefore, I am searching for a build-in method for that purpose.

Thanks,
Ido.
Aug 1 '07 #7
KevinADC
4,059 Expert 2GB
If this is being written specific for windows, the client is most likely using activestate perl. Activestate perl comes with many Win32 modules in it's standard distribution. You can look into using those modules instead of sticking with regular perl core modules if that is the case. Otherwise, File::Find or your own custom function is the only way to go that I know of, or use the operating system as I showed above if that will work.
Aug 1 '07 #8
KevinADC
4,059 Expert 2GB
after a little research, the /V switch (verbose mode, a forward slash followed by an upper case V) seems to do what you want, it returns info like this

Expand|Select|Wrap|Line Numbers
  1. Directory of C:\Perl
  2. File Name         Size        Allocated      Modified      Accessed  Attrib
  3.  
  4.  
  5. .              <DIR>                      12-06-02 10:01p  12-06-02      D      .
  6. ..             <DIR>                      12-06-02 10:01p  12-06-02      D      ..
  7. SITE           <DIR>                      12-06-02 10:01p  12-06-02      D      site
  8. LIB            <DIR>                      12-06-02 10:01p  12-06-02      D      lib
  9. EG             <DIR>                      12-06-02 10:01p  12-06-02      D      eg
  10. BIN            <DIR>                      12-06-02 10:01p  12-06-02      D      bin
  11. HTML           <DIR>                      12-06-02 10:01p  12-06-02      D      html
  12. POD2HTMI X~~        16,683        32,768  12-06-02 10:02p  05-31-07       A     pod2htmi.x~~
  13. POD2HTMD X~~        34,146        49,152  12-06-02 10:02p  05-31-07       A     pod2htmd.x~~
  14. DOCS           <DIR>                      12-06-02 10:08p  12-06-02      D      Docs
  15. PDK            <DIR>                      12-06-02 10:08p  12-06-02      D      PDK
  16.          2 file(s)         50,829 bytes
  17.          9 dir(s)          81,920 bytes allocated
  18.  
so you can get the allocated disk space as well as the file size. The /S switch will drill down into all the sub directories so at the very end you get the totas all summed up:

Expand|Select|Wrap|Line Numbers
  1. Total files listed:
  2.      4,739 file(s)     69,754,995 bytes
  3.      2,174 dir(s)     125,861,888 bytes allocated
  4.                          6,290.38 MB free
  5.                         19,459.84 MB total disk space,  67% in use
  6.  
Aug 1 '07 #9
miller
1,089 Expert 1GB
I have got 1 major constraint: I cannot use an external package such as File::size since I cannot demand from the customer I am doing it for to install that package either. Therefore, I am searching for a build-in method for that purpose.
At the very least then, just look at the source of File::Size in order to determine how they implemented this. File::Find is a core module though, so as Kevin says, you can just roll your own function as well:

Expand|Select|Wrap|Line Numbers
  1. use File::Find;
  2.  
  3. my $directory = shift || '.';
  4.  
  5. print dir_size($directory);
  6.  
  7. sub dir_size {
  8.     my $directory = shift;
  9.     die "Directory expected as parameter" if !-d $directory;
  10.  
  11.     my $size_total = 0;
  12.  
  13.     find({follow => 0, wanted => sub {
  14.         $size_total += -s $File::Find::name || 0;
  15.     }}, $directory);
  16.  
  17.     return $size_total;
  18. }
  19.  
  20. 1;
  21.  
  22. __END__
  23.  
- Miller
Aug 1 '07 #10
KevinADC
4,059 Expert 2GB
That File::Util module Jeff mentioned looks interesting.
Aug 2 '07 #11
idoha
12
At the very least then, just look at the source of File::Size in order to determine how they implemented this. File::Find is a core module though, so as Kevin says, you can just roll your own function as well:

Expand|Select|Wrap|Line Numbers
  1. use File::Find;
  2.  
  3. my $directory = shift || '.';
  4.  
  5. print dir_size($directory);
  6.  
  7. sub dir_size {
  8.     my $directory = shift;
  9.     die "Directory expected as parameter" if !-d $directory;
  10.  
  11.     my $size_total = 0;
  12.  
  13.     find({follow => 0, wanted => sub {
  14.         $size_total += -s $File::Find::name || 0;
  15.     }}, $directory);
  16.  
  17.     return $size_total;
  18. }
  19.  
  20. 1;
  21.  
  22. __END__
  23.  
- Miller
Hi Miller,
This procedure works ok. The only problem is that it return the actual size of directory, not the total space it occupies on the disk.
Aug 2 '07 #12
idoha
12
after a little research, the /V switch (verbose mode, a forward slash followed by an upper case V) seems to do what you want, it returns info like this

Expand|Select|Wrap|Line Numbers
  1. Directory of C:\Perl
  2. File Name         Size        Allocated      Modified      Accessed  Attrib
  3.  
  4.  
  5. .              <DIR>                      12-06-02 10:01p  12-06-02      D      .
  6. ..             <DIR>                      12-06-02 10:01p  12-06-02      D      ..
  7. SITE           <DIR>                      12-06-02 10:01p  12-06-02      D      site
  8. LIB            <DIR>                      12-06-02 10:01p  12-06-02      D      lib
  9. EG             <DIR>                      12-06-02 10:01p  12-06-02      D      eg
  10. BIN            <DIR>                      12-06-02 10:01p  12-06-02      D      bin
  11. HTML           <DIR>                      12-06-02 10:01p  12-06-02      D      html
  12. POD2HTMI X~~        16,683        32,768  12-06-02 10:02p  05-31-07       A     pod2htmi.x~~
  13. POD2HTMD X~~        34,146        49,152  12-06-02 10:02p  05-31-07       A     pod2htmd.x~~
  14. DOCS           <DIR>                      12-06-02 10:08p  12-06-02      D      Docs
  15. PDK            <DIR>                      12-06-02 10:08p  12-06-02      D      PDK
  16.          2 file(s)         50,829 bytes
  17.          9 dir(s)          81,920 bytes allocated
  18.  
so you can get the allocated disk space as well as the file size. The /S switch will drill down into all the sub directories so at the very end you get the totas all summed up:

Expand|Select|Wrap|Line Numbers
  1. Total files listed:
  2.      4,739 file(s)     69,754,995 bytes
  3.      2,174 dir(s)     125,861,888 bytes allocated
  4.                          6,290.38 MB free
  5.                         19,459.84 MB total disk space,  67% in use
  6.  

Hi Kevin,
This could be very helpful for me. I don't really understand how you run this verbose mode. Do you run it on on "dir" command (dir /V ?).
I have been trying to do it and there ain't such an option in XP cmd (only in older version of windows such as 98 and 95).

Any idea?

Thanks,
Ido.
Aug 2 '07 #13
KevinADC
4,059 Expert 2GB
go to the DOS prompt and type:

dir /?

It might be the /n switch with windows XP:

/n : Displays a long list format with file names on the far right of the screen.

windows XP DOS commands

You can try stat()[12] on a file and see if it returns a value. It is the allocated disk space but I don't think it works for Windows:

Expand|Select|Wrap|Line Numbers
  1. $allocated = (stat('c:\filename.txt'))[12];
Aug 2 '07 #14
idoha
12
go to the DOS prompt and type:

dir /?

It might be the /n switch with windows XP:

/n : Displays a long list format with file names on the far right of the screen.

windows XP DOS commands

You can try stat()[12] on a file and see if it returns a value. It is the allocated disk space but I don't think it works for Windows:

Expand|Select|Wrap|Line Numbers
  1. $allocated = (stat('c:\filename.txt'))[12];
Hi,
/n doesn't make any change.
Stat doens't return any allocated blocks as it should.

No cure for me.

Any other suggestions ?

Ido.
Aug 2 '07 #15
KevinADC
4,059 Expert 2GB
maybe fsutil: file

fsutil: file
Aug 2 '07 #16
numberwhun
3,509 Expert Mod 2GB

windows XP DOS commands
Nice link Kevin! Thanks for that!! Always handy to have something like that.

Regards,

Jeff
Aug 2 '07 #17
numberwhun
3,509 Expert Mod 2GB
Hi,
/n doesn't make any change.
Stat doens't return any allocated blocks as it should.

No cure for me.

Any other suggestions ?

Ido.
So you are looking for the number of blocks taken up on the drive? This is the first time you have said that. In the File::Size module you have to define the block size in order to get things like size in MB. If you don't define it, it defaults to 1. As miller said, you may want to examine the source for that package since you cannot use packages.

Regards,

Jeff
Aug 2 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nate Harel | last post by:
Hi all, I need to write a small script that gives me the size of a particular directory. Is there a command or set of commands that will do this? thanks Nate
1
by: Joe | last post by:
I am writing a java application that needs to keep track of what percentage of the disk it uses. It has to run on the various flavors of UNIX and Windows. Problem 1: Is there a Java API class...
4
by: francisl | last post by:
How can we get a full directory size (sum of all his data)? like when we type `du -sh mydir` Because os.path.getsize('mydir') only give the size of the directory physical representation on the...
2
by: Abe | last post by:
I have a strange Perl problem I don't understand. I've written the following program to scan different disks on a Windows server to look for directory files. Works fine until it gets to 'e:' when...
19
by: dchow | last post by:
Our database size is currently 4G and is incrementing at a rate of 45M/day. What is the max size of a SQL database? And what is the size beyond which the server performance will start to go down?
6
by: Hemant Shah | last post by:
Folks, I need to move HOME directory of an instance to another directory. What is the best way of doing it? Is changing password file enough? or dies DB2 store this info in it's own config? ...
4
by: Von Thep via DotNetMonster.com | last post by:
How can I use WMI with VB.NET to get a remote computers directory size? How can I use a unc_path within WMI? Previously I used FileInfo and DirectoryInfo but this method takes too long because...
6
by: titan.nyquist | last post by:
The WebBrowser control won't load a css file written in the same directory as the program. If I put an absolute path to it, it will load it. Thus, the current directory of the WebBrowser control...
3
by: Koliber (js) | last post by:
sorry for my bad english when I fire up (from my c# code) a standard "file - save as " dialog, and when chosen location is a shered local network directory, where I do have rights to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.