2009/3/5 Gunnar Hjalmarsson <[email protected]>:
> Lauri Nikkinen wrote:
>>
>> ... from this script I get
>>
>> The total size of the file in etc is 15712.35 Kb
>> The total size of the file in etc is 15.34 Mb
>>
>> and when I check this from Win XP Explorer (folder properties) window I
>> get
>>
>> Size: 372 KB (380 928 bytes)
>>
>> What is the reason for this difference?
>
> Probably a bug in the code. ;-) Which measures have you taken to try to
> find the bug?
>
Narff. My fault really I should have tried to run the code.
my @directories = grep {! /^\./ if -d} readdir(DIR); should be
my @directories = grep { -d "$path/$_" && ! /^\./ } readdir(DIR);
I've ran this and the results work fine for 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 $path);
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
as 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"
}
sub wanted {
if (-f $_) {
$total_size_of_files_in_dir += -s;
}
}
This will only give you the top level of directories:
07/02/2009 14:00 <DIR> .
07/02/2009 14:00 <DIR> ..
07/02/2009 14:00 <DIR> bin
15/03/2006 22:14 <DIR> eg
07/02/2009 14:00 <DIR> html
15/03/2006 22:15 <DIR> lib
27/04/2005 21:32 <DIR> site
A better solution would show you all the sub-directories. Something to
think about.
Good luck,
Dp.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/