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

Perl Monks, I'm trying to figure out how to skip directories when using file::find. Google isn't turning up any examples of skipping directories and the documentation doesn't explain it all that well....plus im pretty new to perl. Anyone used it and skipped directories because at present im decending into backup directories which is increasing the time taken tenfold! :( Thanks for any info, Lewis

Replies are listed 'Best First'.
Re: Skipping directories using File::Find
by GrandFather (Saint) on Jan 25, 2006 at 10:22 UTC

    Set $File::Find::prune = 1 before you return for a sub-directory tree you wish to skip.


    DWIM is Perl's answer to Gödel
      Im not sure exactly what you mean by that, at what point should i set $File::Find::prune = 1?
      this is what i have:

      find(\&edits, $dir);
      sub edits { push @$cont, $File::Find::name, stat($File::Find::name) if (!($File::Find::name =~ /.snapshot/)); }
        find(\&edits, $dir); sub edits { if ($should_prune) { $File::Find::prune = 1 return; } push @$cont, $File::Find::name, stat($File::Find::name) if (!($File::Find::name =~ /.snapshot/)); }

        DWIM is Perl's answer to Gödel
Re: Skipping directories using File::Find
by zentara (Cardinal) on Jan 25, 2006 at 10:43 UTC
    find (sub { #skip directories which begin with 1 if (-d && $_ =~ /^1.*$/) { $File::Find::prune = 1; return; } #do more stuff here .......... ............ .......... }

    I'm not really a human, but I play one on earth. flash japh
Re: Skipping directories using File::Find
by inman (Curate) on Jan 25, 2006 at 10:52 UTC
    Look at the preprocess option in the documentation.

    The code can be used to sort the file/directory names alphabetically, numerically, or to filter out directory entries based on their name alone.

    You can pre-process a directory and remove entries for backup directories based on a regex or lookup. File::Find will only follow and process the list of files/directories that are returned by this method.

    The example below prints out all files and directories that start with A-D.

    #! /usr/bin/perl use strict; use warnings; use File::Find; find ({wanted => \&wanted, preprocess=> \&preproc, no_chdir=>1}, @ARGV +); sub wanted { print "wanted $_\n" } sub preproc { # print "Pre processing $_\n" foreach (@_); return grep {/^[A-Da-d]/}@_; }
      This approach also has the advantage, that it works with bydepth, while prune does not.
Re: Skipping directories using File::Find
by svenXY (Deacon) on Jan 25, 2006 at 10:22 UTC
    Hi,
    a short find2perl . -type f spits out the following:
    sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ && print("$name\n"); }

    Regards,
    svenXY

    Update: Thoroughly reading the OP after I was downvoted (sorry!), I found that the OP only wants to skip certain directories.
    What I initially wanted to point out is that using the find2perl utility that comes with find2perl spits out nice perl code and can be used to check if the find sub would actually deliver the correct items..
    A correct find2perl line for your problem would then be
    find2perl . -type d -name "1*" -prune -o -type f -print , which would spit out:

    ... sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -d _ && /^1.*\z/s && ($File::Find::prune = 1) || -f _ && print("$name\n"); }

    I just wanted to point out how useful this find2perl can be especially the first couple of times.

Re: Skipping directories using File::Find
by doof (Initiate) on Jan 25, 2006 at 11:14 UTC
    Am i close with this:

    find (sub{
    #skip directories which begin with 1
    if (-d && $_ =~ /^\.snapshot.*$/)
    {
    $File::Find::prune = 1;
    return;
    }

    edits => \&edits


    },$dir);

Re: Skipping directories using File::Find
by Anonymous Monk on Jan 25, 2006 at 11:49 UTC

      It would help others if you showed the important search settings you used as well as the results. Super Search is powerfull, but can generate a lot of noise.

      A useful trick is to set the "Don't include replies" option for a first pass. The search is a lot quicker and less noisy!


      DWIM is Perl's answer to Gödel