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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



etheracide
journeyman

Jul 25, 2004, 4:26 PM

Post #1 of 6 (2441 views)
"Advanced" Directory Size Can't Post

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:

Code
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)


davorg
Thaumaturge / Moderator

Jul 26, 2004, 1:00 AM

Post #2 of 6 (2435 views)
Re: [etheracide] "Advanced" Directory Size [In reply to] Can't Post

I don't really understand why you can't just use the $directory_size variable to sum the directory size. I'd do something like this.


Code
sub get_file_sizes {  
my $directory_size = 0;
opendir (DIRECTORY, "./uploads/$username");

foreach (readdir DIRECTORY) {
# No need for all those extra variables.
# No need for quotes round $file.
my $size = (stat($file))[7];
print "File: $file Size: $size kilobytes"
unless ($file =~ /^\.+$/);
$directory_size += $size;
}
print "Total directory size: $directory_size\n";
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


etheracide
journeyman

Jul 26, 2004, 8:01 AM

Post #3 of 6 (2433 views)
Re: [davorg] "Advanced" Directory Size [In reply to] Can't Post

The reason why I "couldn't" do it that way was basically the = next to the + in adding the $directory_size addition. I had the same thing originally which I described in my post below my code except without the =. When I tried it that way it simply added the first file size to 0 but it then didn't add the second file size to it. For the addition of the second file size it again made $directory_size equal zero and added the size of the second file which negated having ever added the first file size into it.

Now I believe that I not only understand the error of my ways, but will also be able to tackle any problem such as this in the future and I greatly appreciate your help. Cool


etheracide
journeyman

Jul 26, 2004, 7:45 PM

Post #4 of 6 (2429 views)
Re: [davorg] "Advanced" Directory Size [In reply to] Can't Post

The script is printing out all of the text but is not printing any of the filenames or sizes...nor the total directory sizee. I believe that this is because there is no statement 'telling' the script what $file is? In my code, I 'tell' the script what $file is but I do not think the solution does.

Below is what was printed. It definitely seems to me that $file is the culprit. You will notice bellow that the text is printed out 6 times which would suggest that there are 6 files in the directory, but there are only 4. This would lead me to believe that it is taking the "." and ".." into account as the other two files which might suggest that "unless ($file =~ /^\.+$/)" is not working because it is printing even while $file does equal "." and "..".




File: Size: kilobytesFile: Size: kilobytesFile: Size: kilobytesFile: Size: kilobytesFile: Size: kilobytesFile: Size: kilobytesTotal directory size: 0




I am going to attempt working on this but if anyone can point out how to correct the error, please feel free to =0)


(This post was edited by etheracide on Jul 26, 2004, 7:56 PM)


davorg
Thaumaturge / Moderator

Jul 26, 2004, 10:24 PM

Post #5 of 6 (2426 views)
Re: [etheracide] "Advanced" Directory Size [In reply to] Can't Post

Sorry, replace references to "$file" with "$_".

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


etheracide
journeyman

Jul 26, 2004, 11:47 PM

Post #6 of 6 (2425 views)
Re: [davorg] "Advanced" Directory Size [In reply to] Can't Post

It's quite alright! I should've caught that myself but I guess that's part of what happens when PERL is secondary behind actual paid labor. hah But thanks for your help as it is working as intended.

 
 


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

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