I had to add

$total_size_of_files_in_dir = 0;

because it was accumulating. Now it does what is should do and I get the
correct results!!!

---code---
#!/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";
        print "The total size of the file in $dir is " . sprintf("%.2f Mb",
(&size_in_mb($total_size_of_files_in_dir))) . "\n";
        $total_size_of_files_in_dir = 0;
}

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);
}
---code---


2009/3/5 Dermot <paik...@googlemail.com>

> 2009/3/5 Lauri Nikkinen <lauri.nikki...@iki.fi>:
> > Thanks, although is does not change the differences between dir sizes
> from
> > this script and Win Explorer folder properties. This e.g. shows that one
> of
> > my folders has 88 mb size although in fact it is empty. Thank you all,
> I'll
> > give up...
> >
> > -L
> >
>
> You have to be doing something wrong. If I cut and paste the code I
> gave you above and run it, I get:
>
> C:\Documents and Settings\Dermot\Desktop>perl t5.pl c:/perl
> The total size of the file in bin is 2909.41 Kb
> The total size of the file in bin is 2.84 Mb
> The total size of the file in eg is 3519.35 Kb
> The total size of the file in eg is 3.44 Mb
> The total size of the file in html is 27928.85 Kb
> The total size of the file in html is 27.27 Mb
> The total size of the file in lib is 51368.47 Kb
> The total size of the file in lib is 50.16 Mb
> The total size of the file in site is 89741.69 Kb
> The total size of the file in site is 87.64 Mb
>
>
> And this matches what I get from properties.
> Dp.
>
>
> #!/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");
>         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;
>  }
> }
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to