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

In reply to Re: Re: Find the size of sub dirs by Fastolfe
in thread Find the size of sub dirs by roXet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.