When I first learned how to use File::Find, it took some getting used to. The documentation and everything else about it was fine, nevertheless I wrote a wrapper subroutine because I figured it could be made more readable to anyone who read my code but did not understand File::Find (or even perl for that matter).

Here is an example, it might help you, then again it might not.

### begin_: init perl use strict; use warnings; use File::Basename; use File::Find; ### begin_: process all files in the dir tree my $oDataTable = dirTreeToDataTable("c:/temp"); for my $oDataRec (@{$oDataTable}){ next unless $oDataRec->{extension} eq '.html'; ProcessTheFile($oDataRec->{path}); }
### begin_: subroutines ### <region-function_docs> ### main: ### - name : ProcessTheFile ### desc : fill_in_the_blank ### </region-function_docs> sub ProcessTheFile { my $sFilePath = shift || die "missing required argument path +."; ### munge your file however you want ### $sFilePath print "we finished mucking with $sFilePath\n"; }###end_sub ### begin: function docs ### <region-function_docs> ### main: ### - name : dirTreeToDataTable($startdir) ### desc : | ### Recurse all folders and files under start ### directory and return a listing. ### The return listing is a data table. ### Perl Array of Hashes (AoH). ### usage : | ### my $oDataTable = dirTreeToDataTable($startdir); ### </region-function_docs> sub dirTreeToDataTable{ my ($startdir) = @_; my @outArray = (); my $recNum = 0; if($startdir){ $startdir =~ s!(\\)!/!igm; # normalize to unix-style pat +hsteps }else{ return; } &find( sub { my @suffixlist = qw( \..* ); my ($basename,$path,$extension) = File::Basename::file +parse($File::Find::name,@suffixlist); ### INIT my $sTemp = $File::Find::name; my $pathsteps = ''; my $outrec = {}; ### dirTreeToDataTable_datarec_fields_ $outrec->{name} = "$basename$extension"; $outrec->{basename} = "$basename"; $outrec->{extension} = "$extension"; $outrec->{path} = "$File::Find::name"; $outrec->{dir} = "$path"; $outrec->{type} = (-f "$outrec->{path}")? +"file" : "dir"; $outrec->{size} = -s "$File::Find::name"; my @pathsteps = (split m!/!,$outrec->{pa +th}); $outrec->{depth} = scalar @pathsteps; $outrec->{startdir} = $startdir; push @outArray, $outrec; } ,$startdir ); return \@outArray; }###end_sub ### begin_: end perl 1; __END__

In reply to Re: Tutorial on File::Find even more basic than "Beginners Guide" by dimar
in thread Tutorial on File::Find even more basic than "Beginners Guide" by ww

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.