Re: Searching file extensions
by broquaint (Abbot) on Jun 19, 2003 at 11:30 UTC
|
Not being able to spot a single question mark (bar the regex quantifier) I'm not sure what your question is, so here's some code to find extensions instead
use File::Find::Rule;
my @files = find(
file =>
name => [ qw/ *.html *.htm *.pl *.cfm / ],
in => @ARGV
);
This will recursively search for all files ending in html, htm, pl and cfm in all the directories in @ARGV and then assigning the the resulting list of filenames to @files. See. the ever-handy File::Find::Rule for more info.
HTH
_________ broquaint | [reply] [Watch: Dir/Any] [d/l] |
|
My question is how can I put all four of the extensions in my search?
if( $_ =~ /\.html?$/ or \.cfm/ or \.pl/)
Basically get it to search for all four in my "if( $_ =~ /\.html?$/ or \.cfm/ or \.pl/)"
part. I dont think I can put "or" in my search part so how else can I do it? | [reply] [Watch: Dir/Any] [d/l] |
|
A simple use of grouping and alternation should do the trick e.g
print "$_ - ", ( /\.(?:html?|cfm|pl)$/ ? "yep" : "nope" ), "\n"
for qw/ foo.pl bar.cfm baz.htm quux.xxx /;
__output__
foo.pl - yep
bar.cfm - yep
baz.htm - yep
quux.xxx - nope
See. perlre for more info on regexes.
HTH
_________ broquaint | [reply] [Watch: Dir/Any] [d/l] |
Re: Searching file extensions
by Zaxo (Archbishop) on Jun 19, 2003 at 11:32 UTC
|
You can do this with glob:
my @files = glob '*.{html,htm,pl,cfm}';
After Compline, Zaxo
| [reply] [Watch: Dir/Any] [d/l] |
Re: Searching file extensions
by crouchingpenguin (Priest) on Jun 19, 2003 at 12:10 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use constant RECURSIVE => 1;
sub find_files_in_dir {
my ($dir,@extensions) = @_;
my $match = join('|',@extensions);
opendir(DIR,$dir) or die $!;
my @files =
map {
$_->{name} . '.' . $_->{extension};
}
grep {
$_->{extension} =~ m/\A(?:$match)\Z/o if $_;
}
map {
my $file_with_dir = $dir . '/' . $_;
if( -f $file_with_dir ){
{ name => $1, extension => $2} if m/\A(.*?)\.(.*?)\Z/o;
}elsif( RECURSIVE && -d $file_with_dir ){
map {
{name => $1, extension => $2} if m/\A(.*?)\.(.*?)\Z/o;
} find_files_in_dir($file_with_dir , @extensions );
}
} grep { !m/\A(?:\.+)\Z/o } readdir(DIR);
closedir(DIR);
return @files;
}
my @files = find_files_in_dir(
'.',
qw( html pl pm )
);
print Dumper(\@files),"\n";
Update: Added recursion
cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic." | [reply] [Watch: Dir/Any] [d/l] |
|
this returns only the file name, how do i get the filepath along with the file name.
| [reply] [Watch: Dir/Any] |
|
I guess you must have found this ancient thread by searching, so continue your effort and search some more; you will find your answer has already been given here a few hundred times :-)
If you can't find it after trying for a while, post a new question according to the guidelines. (Hint: this question is not likely to get the answer you are seeking.)
The way forward always starts with a minimal test.
| [reply] [Watch: Dir/Any] [d/l] |
|
Re: Searching file extensions
by hardburn (Abbot) on Jun 19, 2003 at 13:53 UTC
|
I had a similar problem a while back, which I solved with a dispatch table. Basically, this is a hash where the values are referances to subroutines. Example:
my %DISPATCH = (
html => \&html,
htm => \&html,
pl => \&pl,
cfm => \&cfm,
);
$_ = '/path/to/file.ext';
$_ =~ /\.(\w+)\z/;
$DISPATCH{$1}->($_, @other_args) if exists $DISPATCH{$1};
sub html { . . . }
sub pl { . . . }
sub cfm { . . . }
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] [Watch: Dir/Any] [d/l] |
|
Why am I recieving the error message:
Can't locate File/Find/Rule.pm in @INC
It didn't seem to have any problem with File:Find
| [reply] [Watch: Dir/Any] |
Re: Searching file extensions
by vek (Prior) on Jun 19, 2003 at 13:21 UTC
|
You've already been given some good ideas about using File::Find::Rule so I though I'd point out something else you could try to find file extensions. You can also use File::Basename with your existing code:
my ($name, $dir, $ext) = fileparse($filepath, '\..*');
--
vek
-- | [reply] [Watch: Dir/Any] [d/l] [select] |