Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello Staralfur,

Another alternative is to use find with multiple parameters and traverse through your directories. But notice that on some systems (such as Cygwin), parentheses are necessary to make the set of extensions inclusive: my @files = `find $path \( -name '*.c' -o -name '*.txt'\)`;.

Sample of script (WRONG see bellow Update2 and Update3):

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $path = shift || '.'; print Dumper traverse($path); sub traverse { my ($path) = @_; my @files = `find $path -name '*.c' -o -name '*.txt'`; return if not -d $path; opendir my $dh, $path or die; while (my $sub = readdir $dh) { next if $sub eq '.' or $sub eq '..'; traverse("$path/$sub"); } close $dh; chomp @files; return \@files; } __DATA__ $ perl file.pl $VAR1 = [ './counts.txt', './file.txt', './sample.c', './testDir/anotherSample.c', './test.txt' ];

But to be 100% honest, I think I would have gone also with stevieb, more generic on OS system. But maybe you can come up with something different.

Update: Combining stevieb solution and File::Find:

#!/usr/bin/perl use strict; use warnings; use File::Find; use Data::Dumper; my @dirs = @ARGV ? @ARGV : ('.'); my @list; find( sub{ push @list, $File::Find::name if -f $_ && $_ =~ /(?:\btest\b|\bsample\b|\bChris\b)/ }, @dirs ); print Dumper \@list; __DATA__ $ perl file.pl $VAR1 = [ './test.pl~', './sample.c', './test.txt~', './test.pl', './test.txt' ];

Update2: Combining stevieb solution and recursive search on directories:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @files; my $path = shift || '.'; print Dumper traverse($path); sub traverse { my ($path) = @_; return if not -d $path; opendir my $dh, $path or die; while (my $sub = readdir $dh) { next if $sub eq '.' or $sub eq '..'; push @files, "$path/$sub" if ("$path/$sub" =~ /(?:\btest\b|\banotherSample\b|\bsample\b) +/); traverse("$path/$sub"); } close $dh; return \@files; } __DATA__ $ perl file.pl $VAR1 = [ './test.pl~', './sample.c', './test.txt~', './testDir/anotherSample.c', './test.pl', './test.txt' ];

Update3: :Another alternative is to use find with multiple parameters and traverse through your directories. But notice that on some systems (such as Cygwin), parentheses are necessary to make the set of extensions inclusive: my @files = `find $path \( -name '*.c' -o -name '*.txt'\)`;.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $path = shift || '.'; my @files = `find $path -name '*.c' -o -name '*.txt'`; chomp @files; print Dumper \@files; __DATA__ $ perl file.pl $VAR1 = [ './test.pl~', './sample.c', './test.txt~', './testDir/anotherSample.c', './test.pl', './test.txt' ];

Hope this helps.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: File::Find find several strings in one directory by thanos1983
in thread File::Find find several strings in one directory by Staralfur

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-08-17 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?