Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a File::Find that works but I only need it to search 3 directories (and their subdirectories) in this '/web/directory'. Right now it searches all 10 of my directories in '/web/directory'. How can I get it to just search the 3 directories (called 'AA', 'BB' and 'CC and all their subdirectories)? MY attempt at it:
#!/usr/local/bin/perl use strict; use File::Find; my $dirs = '/web/directory'; find(\&search, $dirs); sub search { if (-d =~ 'AA' or 'BB' or 'CC') { print "$File::Find::name\n" } }

Replies are listed 'Best First'.
Re: File Find question
by Aristotle (Chancellor) on Nov 06, 2002 at 20:04 UTC
    What you're trying to write there would correctly look like
    #!/usr/local/bin/perl -w use strict; use File::Find; my $dirs = '/web/directory'; find(\&search, $dirs); sub search { print "$File::Find::name\n" if -d and /\A(?:AA|BB|CC)\z/; }
    But why not just say the following?
    #!/usr/local/bin/perl -w use strict; use File::Find; my $dirs = '/web/directory'; find(\&search, map "$dirs/$_", qw(AA BB CC)); sub search { # ... }
    If you don't want any other dirs, why descend anywhere else?

    Makeshifts last the longest.

      thanks but I want the 3 directories and all the subdirectories and all the files under the 3 directories. Sorry I wasnt clear on that earlier. Can you advise how I would do that? Basically what I have currently but just doing the deep searches on files and directories with the 3 I listed.
        As far as I can tell the second snippet I posted will do what you want. If it doesn't, you haven't been clear enough.

        Makeshifts last the longest.

Re: File Find question
by mirod (Canon) on Nov 06, 2002 at 20:44 UTC

    I believe find accepts a list of directories as argument:

    my @dirs = ("/web/directory/AA", "/web/directory/BB", "/web/ +directory/CC"); find(\&search, @dirs);

    You can get fancy by doing:

    my $dir = '/web/directory'; my @subdirs= qw( AA BB CC); my @dirs= map { "$dir/$_"} @subdirs; find(\&search, @dirs);