in reply to Re: Find the size of sub dirs
in thread Find the size of sub dirs

Why are you useing File::Find if you're not actually using it? Your code could be reduced a lot if you used File::Find, and your code would be able to safely handle symlinks and other operating systems.

If your behavior was a little simpler (relying on arguments instead of finding subdirs first):

use File::Find; use strict; foreach my $dir (@ARGV) { my $total; find(sub { $total += -s }, $dir); print "$dir: $total\n"; }
Preserving the behavior of your script (mostly):
use File::Find; use File::Spec; use strict; my $cur = File::Spec->curdir; my $up = File::Spec->updir; foreach my $start (@ARGV) { my @dirs = &find_subdirs($start); foreach my $dir (@dirs) { my $total = 0; find sub { $total += -s }, $dir; printf "%-20s %d\n", $dir, $total; } print STDERR "$start: No subdirectories\n" unless @dirs; } sub find_subdirs { my $start = shift; unless(opendir(D, $start)) { warn "$start: $!\n"; next; } my @dirs = map { -d "$start/$_" && !-l "$start/$_" && $_ ne $cur && $_ ne $up ? "$start/$_" : () } readdir(D); closedir(D); @dirs; }

Replies are listed 'Best First'.
Re: Re: Re: Find the size of sub dirs
by roXet (Initiate) on Feb 12, 2001 at 09:17 UTC
    The reason the File::Find is in there is because I put that script together from a couple of others I was working with, and the use got left in there on accident.