I would definitely recommend using File::Find or one of its variants (and there are some fancy ones) to do the directory scanning. This eliminates the need for you to write any recursive code yourself.

I didn't test the code below and there is bound to be some kind of mistake in it. But this is to give you an idea of another approach.

The most simple variant of File::Find calls a subroutine for every file or directory underneath the starting place. A localized variable $File::Find::name contains the full path of where we currently are (dir name or file name). I suggest that you just run the wanted sub with just the print line at the end (in comments) to see the default order of the descent.

I think this collects the data that you wanted? But not 100% sure that I got everything.

Since you are interested in performance one not so obvious point, is the default _ (underscore) variable. Not $_, just plain "_". When you do a file test operation, all this "stat" info gets collected via a file system request. If you want another file test on the same file, (like -s,-f,-d), using the "_" variable means to use use previous stat info without making another expensive call to the file system.

Hope this at least provides some fuel for thought and further improvement.

#!/usr/bin/perl -w use strict; use File::Find; # find() calls the wanted subroutine for each file and directory # underneath the starting point(s) (can specify more than one # directory tree to traverse down) find(\&wanted, "C:/temp"); # within subroutine "wanted": # use these variables to figure out where you are: # $File::Find::dir is the current directory name, # $_ is the current filename within that directory # $File::Find::name is the complete pathname to the file. # wanted() cannot return anything directly # declare a data struct at a higher scope that this sub # writes to my %mailboxesWithMessages; my %allMailboxes; sub wanted { #full path must contain a mailbox number my $mbox; return() unless ( ($mbox) = $File::Find::name =~ /(\d+)\@/); if (-d $File::Find::name) #some mbox may have no .msg files { $allMailboxes{$mbox}=1; return; } #must be looking at a messsage file return() unless ( -f _ and $File::Find::name =~ /\.msg$/); my $size = -s _; $size = 4096 if $size < 4096; $mailboxesWithMessages{$mbox}+= $size; # comment out above and run this sub with just this line to # see what it does... # print "$File::Find::name\n"; return; }
sort %mailboxesWithMessages to get the biggest one(s). Cycle thorough %allMailboxes - any key there that doesn't exist in the other hash means a mbox with no messages (empty).


In reply to Re: Optimizing performance for script to traverse on filesystem by Marshall
in thread Optimizing performance for script to traverse on filesystem by gdanenb

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.