CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: "Advanced" Directory Size: Edit Log


Jul 25, 2004, 4:26 PM

Views: 5597
"Advanced" Directory Size

I am working on a sub that will get the name and size of each file in a specified directory AND print the total size of all of the files in the directory. The code I will be copying below is a way to print the filenames and their sizes but I am having much difficulty in adding them together correctly for the total directory size. I know that there are other and I am sure better ways of doing what I have coded below but this worked for me. However, I can't figure out how to add all of the file sizes. If anyone would suggest another method which will allow me accomplish both of my goals, i am more then open to suggestions. Code which successfully posts the name and size of each file in a specified directory:

sub get_file_sizes { 
$directory_size = 0;
opendir (DIRECTORY, "./uploads/$username");
@all_files = readdir (DIRECTORY);
closedir (DIRECTORY);
foreach $file (@all_files) {
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks)
= stat("$file");
print "File: $file Size: $size kilobytes" unless ($file =~ /^\.+$/);

You will notice that I set $directory_size to zero in order to prepare for adding all of the file sizes together. I can't add the file size to $directory_size in the foreach loop because the value would just be reset to zero for each file size that is added.

And another note: this is all in an 'included' module and all required information for this code to work is included in the actual script. What I have above DOES work just fine when the script is run, but I can't figure our (correctly) how to add the file size with my method above.

Thank you for any/all suggestions.

(This post was edited by davorg on Jul 26, 2004, 12:55 AM)

Edit Log:
Post edited by davorg (Thaumaturge) on Jul 26, 2004, 12:55 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives