Peamasii has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have searched the archives for getting filesizes with File::Recurse in Windows and found this post:

Re: Filesize counting recursive

but when I tried the code it doesn't work. Getting an error about a HASH ref:

perl mp3size.pl
Directory: ./American Music Club - San Francisco
Can't use string ("./American Music Club - San Fran") as a HASH ref while "strict refs" in use at C:/Perl/site/lib/File/Recurse.pm line 36.

Here is my code. It's supposed to read any directory containing mp3's and sum their filesizes:

use strict; use File::Recurse; my $size; my %files = Recurse(['.'], {match => '\.mp3', nomatch => '\.html$'}); foreach (sort keys %files) { my $directory = $_; print " Directory: $directory\n"; Recurse(\&sum_size, $directory, \$size); print "Total size: $size\n"; } sub sum_size { my ($file, $total_size_ref) = @_; stat($file); unless (-d $_) { $total_size_ref += -s $_; } }

Replies are listed 'Best First'.
Re: getting filesize in windows
by rinceWind (Monsignor) on Aug 23, 2003 at 09:43 UTC
    Peamasii, there's something wierd about that example in the other node. It does not at all match the documented interface for File::Recurse.

    From the pod:

    SYNOPSIS %hash = Recurse(\@dirs, \%rules) Go through the @dirs and apply any rules from %rules. It will return a %hash where the keys are directories, and the values are the files in that directory in an array. e.g. $hash{'/usr/local/bin'} = [ 'ls', 'irc', ... ];
    Nowhere does it mention passing a coderef as the first param.

    I've never used File::Recurse, but you also might want to look at File::Find::Rule.

    --
    I'm Not Just Another Perl Hacker

Re: getting filesize in windows
by thinker (Parson) on Aug 23, 2003 at 10:06 UTC

    Hi peamasii,

    Here is a one liner that will look at all the files under your current directory, and sum the sizes of all files ending in mp3 (or Mp3 or MP3 etc)

    The quotes may need changing for Window's though. I have no means of checking here, but it should be ok to insert into a script

    perl -MFile::Find -le 'find ( sub { $s+= -s if /mp3$/i },  "." );print "size $s"'

    cheers

    thinker

      And since it's on windows, use this version:
      perl -MFile::Find -le "find ( sub { $s+= -s if /mp3$/i }, '.' );print +'size ',$s"

      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/
        The sample worked. In fact I went back to doing this with File::Recurse because the rest of the code used that package. But File::Find essentially did the same recursive search. Thanks