Contributed by SamQi
on Sep 16, 2000 at 11:26 UTC
Q&A
> directories
Description: I've tried piping from find, and I suspect for now it will do.
But I want to recurse a file tree but make sure *not* to recurse
into particular directories. Answer: How do I recurse all *but* a few directories? contributed by Corion The answer you are looking for is the File::Find module. This module
encapsulates recursing through directories
very well.
Some code to get you started :
#!/usr/bin/perl -w
use strict;
use File::Find;
my @searchdirs = ( ".", "c:/", "d:/" );
# And here is the regular expression to check
# for an unwanted directory. In my example,
# I chose the directory name .CVS as an
# "unwanted" directory
my $unwanteddir = ".CVS";
find( sub {
# In this function,we decide what to do
# with each thing we find. We find both,
# files and directories.
# first check, if we have a directory :
if (-d $File::Find::name) {
print "Directory : $File::Find::name\n";
# Now we check if it is unwanted :
if ($File::Find::name =~ m!(^|/)$unwanteddir(/|$)!) {
print "In unwanted directory.\n";
# And tell File::Find that we don't want to look at
# this directory
$File::Find::prune = 1;
};
} elsif (-f $File::Find::name) {
# It's a file
print "File : $File::Find::name\n";
} else {
# It's a symlink or something else we can't handle
};
} , @subdirs ); # here is the end of the find() function call
| Answer: How do I recurse all *but* a few directories? contributed by runrig I think it depends on whether your 'few' directories
are full pathnames or all '.CVS' directories as above.
Or some other requirement we don't know about.
Here's something that includes the first two:
use File::Find;
my @abs_dirs=qw(/dont/go/here /stay/out);
my @all_dirs=qw(.CVS .stayout);
my %abs_dirs;
@abs_dirs{@abs_dirs}=();
my %all_dirs;
@all_dirs{@all_dirs}=()
find (sub {
$File::Find::prune=1, return if -d and (exists $abs_dirs{$File::Find:
+:name} or exists $all_dirs{$_});
# Process other files, dirs
#
}, ".");
Update: runrigs fix (see below) was incorporated
| Answer: How do I recurse all *but* a few directories? contributed by boardryder
use File::Find
my @skip = qw( skip1 skip2 skip3 );
find( \&wanted, $directory );
sub wanted {
return if grep { -d $_ and $_ eq $File::Find::name } @skip;
...
}
|
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
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|