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

Fellow Monks,

I'm using  File::Find to search for certain files on a system. I need to skip over a certain directory. I'm not having much luck trying to do this. Ideally, I would like to say something like next if /$string/, but I know that I read somewhere that you can't use next when not in a loop. Is there someway else I should do this? I would appreciate any help.
use File::Find; use strict; use warnings my @exts = qw(.html .jpg); my $search_root = 'C:\\temp\\'; # Build a Regex my $rexstr=join'|',map {quotemeta $_} @exts; my $rex=qr/(?:$rexstr)$/i; # Directory to ignore my $dir = qr/pics/ find({ wanted =>sub{print "Found a match: $_\n" if (/$rex/);} , no_chdir => 1 }, $search_root);
Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: Ignoring a Directory with File::Find
by fruiture (Curate) on Nov 12, 2002 at 20:13 UTC

    Specify a 'preprocess' routine and filter the directories:

    find( { wanted => sub { ... }, no_chdir => 1, preprocess => sub { grep not m/$dir/ => @_ }, }, $search_root );
    --
    http://fruiture.de
      fruiture,

      Thanks for the reply (as well as everyone else). I tried your code in my script as such:
      # Build a Regex my $rexstr=join'|',map {quotemeta $_} @exts; my $rex=qr/(?:$rexstr)$/i; # Directory to Ignore my $dir = qr/pics/i; my @list; # Find the files find({ wanted =>sub{ print "Found a match: $_\n" if /$rex/; push @list,$_ if /$rex/} , no_chdir => 1, preprocess => sub { grep not /$dir/ => @_ }, }, $search_root );
      but I'm getting the error:
      Not enough arguments for grep at test.pl.txt line 30, near "@_ }" Execution of test.pl.txt aborted due to compilation errors.
      I think my syntax is correct. I did end up taking John M. Dlugosz and FamousLongAgo suggestion and use a blank return statement instead and I got it to do what I want. I'm still curious about this preprocess function though. I could not find any mention to it in the File::Find docs. nor in perlsub Where can I read more on this function?

      Also, I went back and read my node and in my text, and  next if $string, should have read  next if $dir. Hope that didn't confuse anyone.

      Thanks,
      Dru
      Another satisfied monk.
Re: Ignoring a Directory with File::Find
by John M. Dlugosz (Monsignor) on Nov 12, 2002 at 20:24 UTC
    I think what you are trying to do is a return from the callback function. That is, insert return if /$dir/; before the print.

    —John

Re: Ignoring a Directory with File::Find
by FamousLongAgo (Friar) on Nov 12, 2002 at 20:25 UTC
    Instead of next, you can use a bare return:
    find ( { wanted => sub { return if /$string/; ...
    Something to keep in mind, although fruiture shows a cleaner way to do what you want.

      unless you are using InterwovenPerl and thus are stuck 5.005.

      Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Re: Ignoring a Directory with File::Find
by Monky Python (Scribe) on Nov 13, 2002 at 08:54 UTC
    that code works for me!
    find({ wanted =>sub{ if (! ($File::Find::dir =~ /$dir/)) { print "Found a match: $_\n" if (/$rex/);}} , no_chdir => 1 }, $search_root);
    I'm using $File::Find::dir to get the actual directory name. If it matches the $dir regex the directory is ignored.