Hi Monks,

I am an old dog trying to learn some PERL tricks. I am getting back into PERL after more than a decade away. I am attempting to use File::Find to search for multiple patters and return the results as an array.

I have read the Beginners Guide and combed this site but I must be missing something simple?

Here's what I have so far

use strict; use warnings; use Posix; use File::Find; sub findfile { my @Result = (); my $Start = $_[0]; print "Start: [$Start]\n"; if (scalar(@_) >= 2) { foreach my $x (1 .. scalar(@_) - 1) { find( sub { my @finds = grep { $_ =~ /$_[$x]/ } ($File::Fi +nd::name); if (scalar(@finds) > 0) { my $find=$finds[0]; if ($find eq $File::Find::name) { push @Result, $File::Find::name; return; } } }, $Start ); } return @Result; } } my @Paths = findfile("C:/Program Files (x86)/Nimsoft/bin", "*.exe", "* +.txt"); foreach ( @Paths ) { print "$_\n"; }
The output is as follows:
C:/Program Files (x86)/Nimsoft/bin C:/Program Files (x86)/Nimsoft/bin/nimalarm.exe C:/Program Files (x86)/Nimsoft/bin/nimboss.exe C:/Program Files (x86)/Nimsoft/bin/nimbus.exe C:/Program Files (x86)/Nimsoft/bin/nimqos.exe C:/Program Files (x86)/Nimsoft/bin/nimqosdefinition.exe C:/Program Files (x86)/Nimsoft/bin/pu.exe C:/Program Files (x86)/Nimsoft/bin C:/Program Files (x86)/Nimsoft/bin/nimalarm.exe C:/Program Files (x86)/Nimsoft/bin/nimboss.exe C:/Program Files (x86)/Nimsoft/bin/nimbus.exe C:/Program Files (x86)/Nimsoft/bin/nimqos.exe C:/Program Files (x86)/Nimsoft/bin/nimqosdefinition.exe C:/Program Files (x86)/Nimsoft/bin/pu.exe

I am going cross-eyed trying to figure this out. Why am I getting the list of exe files twice? Also, I don't get why the directory itself is printing out?

Any assistance would be very much appreciated

I also know I am showing Windows here. But, if possib;e the routine would be able to run on Linux/Unix as well.


In reply to 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.