larus has asked for the wisdom of the Perl Monks concerning the following question:
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
Where is the error, can you help me?C:\Perl\>perl Print_dirs.pl "C:/Perl/"
#!/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); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Dir size
by shmem (Chancellor) on Mar 05, 2009 at 20:07 UTC | |
by ikegami (Patriarch) on Mar 05, 2009 at 20:48 UTC | |
by Anonymous Monk on Mar 05, 2009 at 20:16 UTC | |
Re: Dir size
by GrandFather (Saint) on Mar 05, 2009 at 22:57 UTC | |
Re: Dir size
by Tanktalus (Canon) on Mar 05, 2009 at 21:52 UTC | |
Re: Dir size
by satanicpuppy (Novice) on Mar 05, 2009 at 22:56 UTC | |
by satanicpuppy (Novice) on Mar 05, 2009 at 23:06 UTC | |
Re: Dir size
by Discipulus (Canon) on Mar 06, 2009 at 08:56 UTC |
Back to
Seekers of Perl Wisdom