Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

recurse directories and sub-directories

by Anonymous Monk
on Oct 12, 2001 at 14:57 UTC ( [id://118444]=perlquestion: print w/replies, xml ) Need Help??

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

How would I recursively open each file in a directory and then its sub directories, etc. performing a process on each file?
  • Comment on recurse directories and sub-directories

Replies are listed 'Best First'.
Re: recurse directories and sub-directories
by broquaint (Abbot) on Oct 12, 2001 at 15:03 UTC
    You'll be a) wanting File::Find and b) actually looking for an answer before asking this all too common question. But if it's desperation you've come to here's some code -
    use File::Find; find(\&wanted, '/usr/local/src'); sub wanted { open(FL, $File::Find::name); # process stuff here # close(FL); }
    HTH

    broquaint

Re: recurse directories and sub-directories
by tomhukins (Curate) on Oct 12, 2001 at 15:02 UTC
Re: recurse directories and sub-directories
by zuqif (Hermit) on Oct 12, 2001 at 16:08 UTC
    just a wee snippet ..
    sub handle_dir { my $d = shift; my $sd = shift || ''; my @all = (); opendir (DIR, $d) or die "Broken!"; while (defined(my $file = readdir(DIR))) { push @all, $file; } chdir ($d); foreach my $file (@all) { next if $file =~ /^\.$/; next if $file =~ /^\.\.$/; my $fp = $d . "\\" . $file; if ($file =~ /htm$/i) { &handle_html ($fp); } if (-d $file) { &handle_dir ($fp); } chdir($d); } chdir(".."); }
    Just a qwik copy & paste .. scuse errors/ommissions the 'qif;

      You're much better off using File::Find than reinventing the wheel like this.

      Firstly, your code will only work on Windows, or any other OS where the directory separator is \. File::Find deals with this transparently. Also you're not checking for failure. What happens if the chdir ($d) fails, maybe because the directory specified in $d doesn't exist?

      The first two lines to match for the current or parent directory could be rewritten as next if $file =~ /^\.{1,2}$/;.

        The first two lines to match for the current or parent directory could be rewritten as next if $file =~ /^\.{1,2}$/;.

        But it would be better not to, because it is a potential time-bomb waiting to explode. Why do people keep dragging out this broken regexp, when a couple of conditionals will do the trick?

        next if $file eq '.' or $file eq '..';

        --
        g r i n d e r
      hokay - well ..
      Initiates rush in where abbots fear to tread I guess
      in future, for sake of any reputation I wanna maintain, will pause before submitting (:
      thems the breaks
      was a snippet from a program to generate an index of a cd of html content ..
      I mean, it works, but s'not exactly value-added-perl
      live n learn etc
      the 'qif;
Re: recurse directories and sub-directories
by George_Sherston (Vicar) on Oct 13, 2001 at 01:43 UTC
    Please do not vote to delete this node without first comparing it carefully with the one which it is erroneously (as the monk who originally proposed it for deletion agrees) supposed to duplicate. They're a pair of links to tie two related threads into one for the benefit of future generations. But they're not duplicates.

    I confess I don't really understand these topics, but I'm reliably informed in the CB that this thread is related to the present one, as well as being a more detailed statement of the question, and may be worth a look.

    § George Sherston

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://118444]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-24 03:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found