Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I recursively count file sizes in a script that works under both Windows and Linux?

( #2311=categorized question: print w/ replies, xml ) Need Help??
Contributed by FiReWaLL on Jan 21, 2000 at 23:20 UTC
Q&A  > directories


Description:

$directory = '/thedir/thatiwantto/count'; print "\n\n"; print CountSize($directory); print "\n\n"; sub CountSize { my ($directory) = $_[0]; my ($ask, $filesize, $item, $size, @raiz); @raiz = glob("$directory/*.*") || warn(); foreach $item (@raiz) { $ask = -d $item; if ($ask) { $size += &CountSize($item); } else { $size += -s $item; } } return($size); }
It should return the total size of all the tree. In Windows, it works, but in Linux, it doesn't. It returns only the current directory file sizes. What should I do?

Answer: How do I recursively count file sizes in a script that works under both Windows and Linux?
contributed by tachyon

File::Find is part of the standard distro, reliable and portable.

use File::Find; my $dir = 'c:/test'; my $size = 0; find( \&size, $dir ); sub size { # we get each filename in $_ my $file_size = -s $_; $size += $file_size; print "Found $_ $file_size Bytes\n"; } print "$size Bytes in '$dir' and all subdirs\n";
Answer: How do I recursively count file sizes in a script that works under both Windows and Linux?
contributed by Crulx

Here is a cross-platform way of doing it.

use strict; my $dir = '.'; print &dir_tree_size($dir) . "\n"; exit 0; sub dir_tree_size { my $dir = shift; my ($i,$total); $total = 0; opendir DIR, $dir; my @files = grep !/^\.\.?$/, readdir DIR; for $i (@files) { if(-d $i) { $total += dir_tree_size($dir . "/$i") } else { $total += -s $i} } return $total; }
Crulx
Answer: How do I recursively count file sizes in a script that works under both Windows and Linux?
contributed by Anonymous Monk

# crulx's solution has a bug - # readdir needs to have the directory glued back # on once you're looking at files outside the # original directory use strict; my $dir = '.'; print &dir_tree_size($dir) . "\n"; exit 0; sub dir_tree_size { my $dir = shift; my ($i,$total, $f); $total = 0; opendir DIR, $dir; my @files = grep !/^\.\.?$/, readdir DIR; for $i (@files) { $f = "$dir/$i"; if(-d $f) { $total += dir_tree_size($f) } else { $total += -s $f} } return $total; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (12)
    As of 2015-07-03 06:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (48 votes), past polls