Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Searching file extensions

by Anonymous Monk
on Jun 19, 2003 at 11:23 UTC ( #267140=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to search for all extension with:
html htm pl cfm
My search is working but the actual finding of file extenstions needs some work. Here is the part I have so far for searching .htm and .html files:
if( $_ =~ /\.html?$/)
#rest of my script here...all works okay

Replies are listed 'Best First'.
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.


      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?
        A simple use of grouping and alternation should do the trick e.g
        print "$_ - ", ( /\.(?:html?|cfm|pl)$/ ? "yep" : "nope" ), "\n" for qw/ bar.cfm baz.htm /; __output__ - yep bar.cfm - yep baz.htm - yep - nope
        See. perlre for more info on regexes.


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,

Re: Searching file extensions
by crouchingpenguin (Priest) on Jun 19, 2003 at 12:10 UTC

    How about this (as an alternative to the File::Find answers)?

    #!/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

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
      this returns only the file name, how do i get the filepath along with the file name.

        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.
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

      Why am I recieving the error message: Can't locate File/Find/ in @INC It didn't seem to have any problem with File:Find
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 --

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://267140]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2016-08-25 14:22 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (360 votes). Check out past polls.