in reply to Finding files with multiple patterns

I started to walk through the logic, and explain why it's not working as you would like, but there were more than a couple smoking guns, and finally I decided to show you one way I might do it, if I were to use File::Find for this.

I will mention however, that your use of grep is incorrect (you should be passing a useful list to it). You would also avoid some confusion by unpacking your function's args list at the top, as $_[0] within your anonymous sub is not referring to the parent sub's arg list. Also, unless you test whether the thing that File::Find is looking at is a file, you will see directories too. Furthermore, "*.exe", used as a regexp pattern isn't doing what you want.

The following has been tested on Linux, with an appropriate change to the target starting path. ;)

use File::Find; my @files_found = find_files( 'C:/Program Files (x86)/Nimsoft/bin', [ qr/\.exe$/, qr/\.txt$/ ] ); print "$_\n" for @files_found; sub find_files { my( $start, $patterns_aref ) = @_; my @found; find( sub { if( -e && -f ) { for my $pattern ( @$patterns_aref ) { if( m/$pattern/ ) { push @found, $File::Find::name; last; # Matched, so we don't need to test other rules. } } } }, $start ); return @found; }

It should work on Windows as well.

Update: I mentioned at the outset that this is one way I might do it if I were to use File::Find. I said that because I would probably prefer using the cleaner feeling File::Find::Rule instead; although I haven't worked through the solution, I suspect it would turn out easier to read.


Dave

Replies are listed 'Best First'.
Re^2: Finding files with multiple patterns
by pgduke65 (Acolyte) on May 15, 2013 at 01:51 UTC

    Thanks for the reply. I appreciate the help. Apparently I am more rusty than I thought :).

      Keep at it! :) You're on your way.


      Dave