473,396 Members | 1,712 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,396 software developers and data experts.

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 16422
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

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

Similar topics

6
by: User | last post by:
Anyone have ideas which os command could be used to get the size of a file without actually opening it? My intention is to write a script that identifies duplicate files with different names. I...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
0
by: PeterB | last post by:
Hi! I am using the script below to download files, that are in a non-public directory, from my site. The "smaller files" section works for smaller files. However when the files are getting...
9
by: minil | last post by:
Hi any c function in linux to return size of directory (Including its files & subdirectories & its files).. I can stat() only files . not for directories. please reply me soon Thanks in...
1
by: JJ | last post by:
How do I get the immediate subdirectories and their size under a given directory? I just want list of folders only one level down and total size of each folder under the parent directory? ...
10
by: Pieter Coucke | last post by:
Hi, I need to find all the files in a directory which are younger than a given date (modification date or creation date). Also I need to find a way to find all files in a directory which are...
3
by: TyBreaker | last post by:
I have a vbscript which returns the folder size of any folder instantly by referring to Folder.Size. I don't need any iteration or recursion through the subfolders, adding together individual file...
12
by: garyusenet | last post by:
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.
3
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, Can anyone explain me the easiet way to get the size of a folder, including all files subfolders and. Something like DirectoryInfo.Size, helas, its not there. regards Jesper.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.