I'm trying to print out all the directories and their sizes using the script below but I'm not getting correct results (I got some advice from http://www.mail-archive.com/beginners@perl.org/msg99758.html). For example it says that my C:/Perl/etc directory is 15.32 mb but Win Explorer folder properties says that it's 372 kb. I'm using Win XP and the script like this
C:\Perl\>perl Print_dirs.pl "C:/Perl/"
Where is the error, can you help me?
#!/bin/perl
use warnings;
use strict;
use File::Find;
my $path = $ARGV[0];
die "You must supply a full directory path" unless (-e $path && -d $pa
+th);
opendir (DIR, $path) or die "can't opendir $path: $!";
my @directories = grep { -d "$path/$_" && ! /^\./ } readdir(DIR);
my $total_size_of_files_in_dir;
foreach my $dir (@directories) {
find(\&wanted, "$path/$dir"); ### Not sure how that worked a
+s you called it $directory
print "The total size of the file in $dir is " . sprintf("%.2f
+ Kb", ($total_size_of_files_in_dir * 0.0009765625)) . "\n";
print "The total size of the file in $dir is " . sprintf("%.2f
+ Mb", ($total_size_of_files_in_dir * 9.5367431641e-7)) . "\n";
print "The total size of the file in $dir is " . sprintf("%.2f
+ Mb", (&size_in_mb($total_size_of_files_in_dir))) . "\n";
}
sub wanted {
if (-f $_) {
$total_size_of_files_in_dir += -s;
}
}
sub size_in_mb {
my $size_in_bytes = shift;
return $size_in_bytes / (1024 * 1024);
}
In reply to Dir size
by larus
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.