Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How do I recurse all *but* a few directories?

by SamQi (Beadle)
on Sep 16, 2000 at 11:26 UTC ( [id://32789]=perlquestion: print w/replies, xml ) Need Help??

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

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.

Originally posted as a Categorized Question.

  • Comment on How do I recurse all *but* a few directories?

Replies are listed 'Best First'.
Re: How do I recurse all *but* a few directories?
by Corion (Patriarch) on Sep 16, 2000 at 12:21 UTC

    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
Re: How do I recurse all *but* a few directories?
by runrig (Abbot) on Sep 16, 2000 at 22:21 UTC
    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

      '$File::Find::dir' above should be '$File::Find::name', since '...dir' gives you the directory above the current directory '$_'.

      Too bad we don't get to edit Answers :(
Re: How do I recurse all *but* a few directories?
by boardryder (Novice) on Jul 04, 2009 at 01:05 UTC
    use File::Find my @skip = qw( skip1 skip2 skip3 ); find( \&wanted, $directory ); sub wanted { return if grep { -d $_ and $_ eq $File::Find::name } @skip; ... }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found