in reply to Limiting file:find depth question

If I'm understanding correctly...
#!/usr/bin/perl # dirpath use strict; use warnings; use File::Find; #*****************Path Variables********************** our $testpath = 'C:\\Temp\\'; #******************************************************* find ({ wanted => \&wanted, preprocess => \&preprocess }, $testpath); sub wanted { return unless -d; print "$File::Find::dir/$_\n" if /[IPD]\d{8}$/; } sub preprocess { if ($File::Find::dir =~ /[IPD]\d{8}$/) { return; } else { return @_; } }

Replies are listed 'Best First'.
Re^2: Limiting file:find depth question
by Anonymous Monk on Nov 14, 2013 at 08:57 UTC

    The same thing using File::Find::Rule

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd pp /; use Path::Tiny qw/ path /; use File::Find::Rule qw/ find /; my $thisdir = shift || path( __FILE__ )->parent; my @all = File::Find::Rule->in( "$thisdir" ); my @dirs = File::Find::Rule->directory ->name( qr/^[IPD]\d{8}$/ ) ->prune ## don't descend ->in( $thisdir ); dd( \@all, \@dirs ); dd( find( directory => name => qr/^[IPD]\d{8}$/, prune => ## don't descend in => $thisdir, ) ); __END__ $ perl ffindrule.pl . ( [ ".", "ffindrule.pl", "a", "a/echo.txt", "a/q", "a/q/echo.txt", "a/r", "a/r/D20131114", "a/r/D20131114/fa", "a/r/I20131114", "a/r/I20131114/fa", "a/r/P20131114", "a/r/P20131114/fa", "a/r/P20131114/P20131114", "b", "b/echo.txt", "b/s", "b/s/D20131114", "b/s/D20131114/away", "b/s/I20131114", "b/s/I20131114/ru", "b/s/P20131114", "b/s/P20131114/P20131114", "b/s/P20131114/run", "b/s/t", "b/s/t/o", "b/s/t/o/p", "b/s/t/o/p/D20131114", "b/s/t/o/p/I20131114", "b/s/t/o/p/P20131114", "b/t", "b/t/echo.txt", ], [ "a/r/D20131114", "a/r/I20131114", "a/r/P20131114", "b/s/D20131114", "b/s/I20131114", "b/s/P20131114", "b/s/t/o/p/D20131114", "b/s/t/o/p/I20131114", "b/s/t/o/p/P20131114", ], ) ( "a/r/D20131114", "a/r/I20131114", "a/r/P20131114", "b/s/D20131114", "b/s/I20131114", "b/s/P20131114", "b/s/t/o/p/D20131114", "b/s/t/o/p/I20131114", "b/s/t/o/p/P20131114", )

    http://p3rl.org/Data::Dump http://p3rl.org/Path::Tiny http://p3rl.org/File::Find::Rule

Re^2: Limiting file:find depth question
by RockE (Novice) on Nov 14, 2013 at 06:09 UTC

    Don't fully understand whats going on here just yet, but I've tried it and it works quite a bit quicker than what I had, so thankyou.

      The preprocess sub is called for each directory that File::Find descends into. It's called before the wanted sub as a sort of filter. $File::Find::dir contains the name of the current directory being processed, including any leading path...like '/foo/bar/the-current-directory'.

      @_ contains the list of files/directories in this current directory. You choose what portion of @_ you return. If you return @_...it's a no-op, things go as normal. If you return nothing, then it will not descend below this current directory, and will not return the files in the current directory either. If you return some subset of @_, it will descend into only those directories, and only pass those files to the wanted sub.

      The code I gave won't likely work exactly for what you're trying to do, as I'm not sure I fully understand what you're doing. You'll need to figure out what you want to filter out of @_. As written, it returns nothing if the pattern matches, pruning at that point, and everything if the pattern doesn't match.