Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I am writing a script that can take the search pattern as an argument and list all the files that match that pattern in a specified directory.
The below script works fine as long as I provide the search pattern as words or numbers.
use strict; my $wildcard = $ARGV[0]; print "wildcard is $wildcard\n"; my $filespath = '/path/to/files/'; opendir DIR, $filespath or die "Couldn't open $filespath: $!\n"; # Sort by asc order (time) # (The commented line below sorts all the files that end with ".cgi" #my @dirfiles = sort { -M $filespath.$a <=> -M $filespath.$b } grep /\ +.cgi$/, readdir DIR; my @dirfiles = sort { -M $filespath.$a <=> -M $filespath.$b } grep /$w +ildcard/, readdir DIR; foreach (@dirfiles){ print "$filespath$_\n" }
How do I make it work for patterns such as "a*sample*.cgi" (the string to match is within the quotes)?
Thanks, Vgn
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Wildcard search in a directory
by graff (Chancellor) on Jul 05, 2007 at 18:13 UTC | |
by Anonymous Monk on Jul 05, 2007 at 18:35 UTC | |
by Joost (Canon) on Jul 05, 2007 at 19:18 UTC | |
|
Re: Wildcard search in a directory
by Tux (Canon) on Jul 05, 2007 at 17:26 UTC | |
|
Re: Wildcard search in a directory
by citromatik (Curate) on Jul 06, 2007 at 09:03 UTC |