Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How do I recursively process every file in a given directory?

by vroom (His Eminence)
on Jan 08, 2000 at 08:22 UTC ( [id://1872]=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (directories)

How do I recursively process every file in a given directory?

Originally posted as a Categorized Question.

  • Comment on How do I recursively process every file in a given directory?

Replies are listed 'Best First'.
Re: How do I recursively process every file in a given directory?
by Dominus (Parson) on Mar 12, 2000 at 02:24 UTC
    use File::Find; find (\&wanted, $DIRECTORY); sub wanted { # this subroutine gets called once for each file # Inside this subroutine, $_ is set to the # name of the current file and the current # directory is the directory where that file # is stored }

    See the manual for File::Find for more complete details.

Re: How do I recursively process every file in a given directory?
by marcos (Scribe) on May 02, 2000 at 14:15 UTC
    I would use opendir and readdir:
    opendir (DIR, $dir) or die "cannot opendir $dir"; foreach my $file (readdir(DIR)) { &process_file ($file); } closedir (DIR);

    or to process ONLY files:
    my @only_files = grep {-f "$dir/$_"} readdir(DIR); foreach my $file (@only_files) { &process_file ($file); }

    and finally a one-line-do-everything version (my favorite one):
    map {&process_file} grep {-f "$dir/$_"} readdir(DIR);

    marcos
Re: How do I recursively process every file in a given directory?
by tenatious (Beadle) on Jun 22, 2000 at 18:47 UTC

    You can also use File::Recurse

    use File::Recurse; recurse(\&function, "/path/to/directory/to/recurse"); exit; sub function { shift; print "$_\n" ; }
Re: How do I recursively process every file in a given directory?
by a_login (Initiate) on Jan 07, 2002 at 07:27 UTC
    tenatious:

    i tried your code and it wouldn't work for me. first the function is Recurse and not recurse. the arguments to the function are a reference to an array of directories and a reference to a hash of options.

    while playing with it i came up with:
    #! /usr/bin/perl use strict; use warnings; MAIN: { my @SDIRS; my %dirs; my %rules; $SDIRS[0] = "./"; #$rules{match} =; use File::Recurse; %dirs = Recurse(\@SDIRS, \%rules); my ($key, $value, @atmp); while (($key,$value) = each %dirs) { @atmp = @{$value}; foreach(@atmp){ #here $key is the path to a file #and $_ is the file itsself print $key."/".$_."\n"; } } exit 0; }
Re: How do I recursively process every file in a given directory?
by crazyinsomniac (Prior) on May 11, 2002 at 02:45 UTC

    Re: How do I recursively process every file in a given directory?

    Originally posted as a Categorized Answer.

Re: How do I recursively process every file in a given directory?
by Anonymous Monk on May 11, 2002 at 01:23 UTC
    hehe, its my coffee brake so i couldnt resist to bake and submit a homegrown.
    it aint pretty and the disclaimer took more mork then the code, but hey , i had three more minutes when it wuz finsihed !
    yippy! back to work, if ya like going on holidays you need to get a new job folks, computers are sweet.
    i'd just wish atari still survived, or amiga, or .. ohwell, *sigh*, ... i hope this helps someone out there :)
    if it does, please enjoy but above all:
    be nice
    .recurser.pl: #!/usr/bin/perl # automagical-develop-and-do-what-you-want # licenced by your monthly # abZurd.org or abZurd-affiliate visit ;) # http://abzurd.com/legal/licence/automagical # easily install by following next three steps # step 1: # make sure you are programming in perl # yes?, ok, place sub in code :] sub recurse { my $dir = shift; my $action = shift; print "this isn't a dir! ($dir)\n" unless (-d $dir); opendir(DIR,$dir); foreach(readdir(DIR)) { if ($_ =~ /^\..?.?$/) { next; } if (-d "$dir/$_") { &recurse("$dir/$_",$action); } else { &$action("$dir/$_"); } } } # onwarths with the fun part # step 2: # create a sub that does something with all those # digital hunks of data (all bought and paid are they?) sub doMeCauseItHurts { printf "%s\n",shift; # ohyeah,ls -R|dir/s functionality ;) } # step 3: # just call it on the dir and pass it a reference to your sub: &recurse($ENV{USERPROFILE},\&doMeCauseItHurts);

    Originally posted as a Categorized Answer.

Re: How do I recursively process every file in a given directory?
by Anonymous Monk on May 11, 2002 at 01:37 UTC
    Oops, excuse me
    i made a booboo, the regexp is wrong
            if ($_ =~ /^\..?.?$/) { next; }
    
    is wrong and should be replaced by
         if ($_ =~ /^\..?.?$/) { next; }
    
    hehe

    don't i look silly now ;)
    Anonymous abZurdist of a Monk

    Originally posted as a Categorized Answer.

Re: How do I recursively process every file in a given directory?
by Anonymous Monk on May 11, 2002 at 01:42 UTC
      if ($_ =~ /^\.\.?\.?$/) { next; }
    
    hehe, AZM :/

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2025-07-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.