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


In reply to Re: Finding files with multiple patterns by davido
in thread Finding files with multiple patterns by pgduke65

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.