Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by vroom (His Eminence)
on Jan 08, 2000 at 08:22 UTC ( #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; }

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? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2022-01-23 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?