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

I am searching many directories but dont want to include all directories in my search. Can I use 'File::find::prune' to eliminate directories I dont want included in my search? Here is my attempt that is not working trying to search directories except 'dirOne', 'dirTwo' and 'dirThree':
use strict; use File::Find; my $startDir = '/mypathhere'; unless (-d $startDir) { die "Directory '$startDir' is not a Directory.\n"; } find(\&processSub, $startDir); sub processSub { if(-d $_) { #I DONT WANT TO SEARCH IN 'dirOne' and 'dirTwo' and 'dirThree' $File::find::prune = '/dirOne', '/dirTwo', 'dirThree'; print "$File::Find::name\n"; } }

Replies are listed 'Best First'.
Re: Prune File Find search
by broquaint (Abbot) on Jul 28, 2003 at 15:13 UTC
    You could just use the preprocess argument
    find({ preprocess => sub { return grep { not(-d and /dir(?:One|Two|Three)/) } @_ }, wanted => \&processSub, }, $startDir);
    Or (you knew it was coming folks) with File::Find::Rule
    use File::Find::Rule; my $iter = rule( not => rule( directory => name => qr/dir(?:One|Two|Three)/, prune => ), start => '/your/path/here' ); while(my $thing = $iter->match) { ... }
    See. File::Find::Rule docs for more info.
    HTH

    _________
    broquaint

      thanks, i tried this and it still prints 'dirOne','dirTwo', and 'dirThree'. Please advise. Thanks
      use strict; use File::Find; my $startDir = '/mypathhere'; unless (-d $startDir) { die "Directory '$startDir' is not a Directory.\n"; } find({ preprocess => sub { return grep { not(-d and /dir(?:dirOne|dirTwo|dirThree)/) } @_ }, wanted => \&processSub, }, $startDir); sub processSub { if(-d $_) { print "$File::Find::name\n"; } }
        Yes ... that's because you changed the regexp in the grep, it should be
        preprocess => sub { ## added ^ and $ return grep { not(-d and /^dir(?:One|Two|Three)$/) } @_ },
        See. your local perlre for more info on alternations (the | metacharacter), and anchors (as seen by the ^ and $ in the regexp).
        HTH

        _________
        broquaint

Re: Prune File Find search
by Anonymous Monk on Jul 28, 2003 at 15:28 UTC
    File::Find::Rule does not install on win32 machines... BTW. Murat
      Make sure you've also got Number::Compare and Text::Glob installed as it depends on those modules. Otherwise it should be find since it's a plain perl file and has no particular OS ties.
      HTH

      _________
      broquaint

      File::Find::Rule version 0.10 installs fine for me on activestate's latest 5.006_001 and 5.008 perls, on win2000 and winxp via perl -MCPAN -e"install File::Find::Rule".

      ~Particle *accelerates*

        Whelp it sure dont on my system. :-) And im pretty sure I reported this. But I dont remember for sure. BTW, yes I know that the latest 5.6.1 is 635, however as _that_ build breaks Inline::CPP for unknown reasons I have no intention of "upgrading" to it for File::Find::Rule.

        D:\Development\OldEverest\C++\bin>perl -MCPAN -e"install File::Find::R +ule" CPAN: Storable loaded ok Going to read E:\.cpan\Metadata Database was generated on Thu, 17 Jul 2003 19:45:08 GMT CPAN: LWP::UserAgent loaded ok Fetching with LWP: ftp://ftp.gmd.de/mirrors/CPAN/authors/01mailrc.txt.gz Going to read E:\.cpan\sources\authors\01mailrc.txt.gz CPAN: Compress::Zlib loaded ok Fetching with LWP: ftp://ftp.gmd.de/mirrors/CPAN/modules/02packages.details.txt.gz Going to read E:\.cpan\sources\modules\02packages.details.txt.gz Database was generated on Sun, 27 Jul 2003 17:16:21 GMT CPAN: HTTP::Date loaded ok There's a new CPAN.pm version (v1.71) available! [Current version is v1.59_54] You might want to try install Bundle::CPAN reload cpan without quitting the current session. It should be a seamless upgrad +e while we are running... Fetching with LWP: ftp://ftp.gmd.de/mirrors/CPAN/modules/03modlist.data.gz Going to read E:\.cpan\sources\modules\03modlist.data.gz Going to write E:\.cpan\Metadata Running install for module File::Find::Rule Running make for R/RC/RCLAMP/File-Find-Rule-0.10.tar.gz CPAN: MD5 loaded ok Checksum for E:\.cpan\sources\authors\id\R\RC\RCLAMP\File-Find-Rule-0. +10.tar.gz ok Scanning cache E:\.cpan\build for sizes Using external gzip/tar combination Its compressed, using a pipe. File-Find-Rule-0.10/ File-Find-Rule-0.10/Extending.pod File-Find-Rule-0.10/t/ File-Find-Rule-0.10/t/lib/ File-Find-Rule-0.10/t/lib/File/ File-Find-Rule-0.10/t/lib/File/Find/ File-Find-Rule-0.10/t/lib/File/Find/Rule/ File-Find-Rule-0.10/t/lib/File/Find/Rule/Test/ File-Find-Rule-0.10/t/lib/File/Find/Rule/Test/ATeam.pm File-Find-Rule-0.10/t/File-Find-Rule.t File-Find-Rule-0.10/t/foobar File-Find-Rule-0.10/MANIFEST File-Find-Rule-0.10/findrule File-Find-Rule-0.10/Rule.pm File-Find-Rule-0.10/Changes File-Find-Rule-0.10/Makefile.PL File-Find-Rule-0.10/README Removing previously used E:\.cpan\build\File-Find-Rule-0.10 CPAN.pm: Going to build R/RC/RCLAMP/File-Find-Rule-0.10.tar.gz Checking if your kit is complete... Looks good Writing Makefile for File::Find::Rule Microsoft (R) Program Maintenance Utility Version 7.00.9466 Copyright (C) Microsoft Corporation. All rights reserved. cp Rule.pm blib\lib\File\Find/Rule.pm cp Extending.pod blib\lib\File\Find/Rule/Extending.pod E:\Perl\bin\perl.exe -IE:\Perl\lib -IE:\Perl\lib -MExtUtils::C +ommand -e cp findrule blib\script\findrule E:\Perl\bin\perl.exe -IE:\Perl\lib -IE:\Perl\lib -e "system q +q[pl2bat.bat ].shift" blib\script\findrule E:\DotNet\VC7\BIN\nmake.EXE -- OK Running make test Microsoft (R) Program Maintenance Utility Version 7.00.9466 Copyright (C) Microsoft Corporation. All rights reserved. E:\Perl\bin\perl.exe -IE:\Perl\lib -IE:\Perl\lib -MExtUtils::C +ommand -e cp findrule blib\script\findrule E:\Perl\bin\perl.exe -IE:\Perl\lib -IE:\Perl\lib -e "system q +q[pl2bat.bat ].shift" blib\script\findrule E:\Perl\bin\perl.exe -Mblib -IE:\Perl\lib -IE:\Perl\lib -e "us +e Test::Harness qw(&runtests $verbose); $verbose=0 ; runtests @ARGV;" t\File-Find-Rule.t Using E:/.cpan/build/File-Find-Rule-0.10/blib t\File-Find-Rule....ok 36/40# Failed test (t\File-Find-Rule.t at l +ine 281) # Structures begin differing at: # $got->[0] = 'unknown option 'foobar'' # $expected->[0] = 't/File-Find-Rule.t' t\File-Find-Rule....NOK 37# Failed test (t\File-Find-Rule.t at lin +e 284) # Structures begin differing at: # $got->[0] = 'unknown option 'foo'' # $expected->[0] = 't/foobar' t\File-Find-Rule....ok 40/40# Looks like you failed 2 tests of 40. t\File-Find-Rule....dubious Test returned status 2 (wstat 512, 0x200) DIED. FAILED tests 37-38 Failed 2/40 tests, 95.00% okay Failed Test Stat Wstat Total Fail Failed List of Failed ---------------------------------------------------------------------- +--------- t\File-Find-Rule.t 2 512 40 2 5.00% 37-38 Failed 1/1 test scripts, 0.00% okay. 2/40 subtests failed, 95.00% okay +. NMAKE : fatal error U1077: 'E:\Perl\bin\perl.exe' : return code '0x2' Stop. E:\DotNet\VC7\BIN\nmake.EXE test -- NOT OK Running make install make test had returned bad status, won't install without force D:\Development\OldEverest\C++\bin>perl -v This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2001, Larry Wall Binary build 633 provided by ActiveState Corp. http://www.ActiveState. +com Built 21:33:05 Jun 17 2002 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge.

        ---
        demerphq

        <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

        And I just found the bug too. In .\File-Find-Rule-0.10\t\File-Find-Rule.t there is a subroutine called 'run'. It says:

        $expr =~ s~\\~~g if ($^O eq 'Win32' || $^O eq 'dos');
        Which is wrong:
        D:\>perl -e "print $^O" MSWin32 D:\>e:\perl805\bin\perl -e "print $^O" MSWin32
        It should IMO read
        $expr =~ s~\\~~g if ($^O eq 'MSWin32' || $^O eq 'dos');

        And frankly given this bug I manifestly fail to see how it installed successfully on the systems you have listed. Are you sure you installed 0.10 which is the latest? Can you please prove your claim? Along with the value of $^O on your various installs?


        ---
        demerphq

        <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

        • Update:  
        Reported this to the author via RT.