469,292 Members | 1,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

Calculate size of all files in a Directory

Hi ,
Can anyone help me in finding the number of files and their size in Directory.
Feb 16 '07 #1
3 15878
miller
1,089 Expert 1GB
You simply need to read in the contents of the directory and add up the size of each of the files. Pertinent documentation can be found here:

http://perldoc.perl.org/functions/readdir.html - read in the files
http://perldoc.perl.org/functions/-X.html - obtain the size of the file
http://perldoc.perl.org/functions/stat.html - alternate method for file size.
Feb 16 '07 #2
KevinADC
4,059 Expert 2GB
I find stuff like this easier to do with a shell command but if it needs to be portable then use the File::Find module or write your own function to recurse directories and add up the file sizes. You can use the -s file test operator to get the size.
Feb 16 '07 #3
Hi ,
Can anyone help me in finding the number of files and their size in Directory.
You do not even need a module:
Expand|Select|Wrap|Line Numbers
  1. sub dirSize {
  2.   my($dir)  = @_;
  3.   my($size) = 0;
  4.   my($fd);
  5.  
  6.   opendir($fd, $dir) or die "$!";
  7.  
  8.   for my $item ( readdir($fd) ) {
  9.     next if ( $item =~ /^\.\.?$/ );
  10.  
  11.     my($path) = "$dir/$item";
  12.  
  13.     $size += ((-d $path) ? dirSize($path) : (-f $path ? (stat($path))[7] : 0));
  14.   }
  15.  
  16.   closedir($fd);
  17.  
  18.   return($size);
  19. }
  20.  
Greetz, Doc
Feb 19 '07 #4

Post your reply

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

Similar topics

6 posts views Thread by User | last post: by
9 posts views Thread by minil | last post: by
1 post views Thread by JJ | last post: by
10 posts views Thread by Pieter Coucke | last post: by
3 posts views Thread by TyBreaker | last post: by
3 posts views Thread by =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.