473,382 Members | 1,407 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,382 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 16416
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
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);
  6.   opendir($fd, $dir) or die "$!";
  8.   for my $item ( readdir($fd) ) {
  9.     next if ( $item =~ /^\.\.?$/ );
  11.     my($path) = "$dir/$item";
  13.     $size += ((-d $path) ? dirSize($path) : (-f $path ? (stat($path))[7] : 0));
  14.   }
  16.   closedir($fd);
  18.   return($size);
  19. }
Greetz, Doc
Feb 19 '07 #4

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

Similar topics

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...
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...
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...
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...
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? ...
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...
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...
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.
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.
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.