Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

( #1872=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 08, 2000 at 08:22 UTC
Q&A  > directories


Answer: How do I recursively process every file in a given directory?
contributed by Dominus

	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.

Answer: How do I recursively process every file in a given directory?
contributed by marcos

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
Answer: How do I recursively process every file in a given directory?
contributed by tenatious

You can also use File::Recurse

use File::Recurse; recurse(\&function, "/path/to/directory/to/recurse"); exit; sub function { shift; print "$_\n" ; }
Answer: How do I recursively process every file in a given directory?
contributed by a_login

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; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (12)
    As of 2015-07-07 22:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls