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. | [reply] [d/l] |
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 | [reply] [d/l] [select] |
use File::Recurse;
recurse(\&function, "/path/to/directory/to/recurse");
exit;
sub function {
shift;
print "$_\n" ;
}
| [reply] [d/l] |
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;
}
| [reply] [d/l] |
Re: How do I recursively process every file in a given directory?
Originally posted as a Categorized Answer. | [reply] |
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. | [reply] [d/l] |
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. | [reply] |
if ($_ =~ /^\.\.?\.?$/) { next; }
hehe, AZM :/
Originally posted as a Categorized Answer. | [reply] |