Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Extracting just the extension from a filename.

by brentheigold (Novice)
on Jul 19, 2002 at 19:08 UTC ( [id://183387]=perlquestion: print w/replies, xml ) Need Help??

brentheigold has asked for the wisdom of the Perl Monks concerning the following question:

I have a file called mmddyyyy.csv, so say its like:
my ($fileName) = "20020719.csv"
How do I just take the "csv" from the filename, so i guess its a matter of choppping off everything up to and including the "." Would appreciate any help, thanks a lot. Brent.

Replies are listed 'Best First'.
Re: Extracting just the extension from a filename.
by ichimunki (Priest) on Jul 19, 2002 at 19:33 UTC
    With a core module: File::Basename-- although this leaves the dot on the suffix, so you'll still want to use s/^\.// on the results on the call to fileparse().
Re: Extracting just the extension from a filename.
by insensate (Hermit) on Jul 19, 2002 at 19:16 UTC
    Try something like this:
    my $fileName = "20020719.csv"; $fileName =~ /\.(\w+)$/; my $extension = $1;

    Jason

      This would work OK, but if the extension would contain some `funny' characters you could get into problem. It is not very often that you get extensions with spaces and other weird things, but hey, you never know. So my solution would be:

      my $filename = '20020719.csv'; $filename =~ /\.([^\.]*)$/; my $extension = $1;
      Good solution. My only additions would be to use \^\.\ instead of \w to catch \W as well but not ".".

      You could also one-line it by catching the matches with alist assignment to avoid reading from $1:

      my ($extension) = $filename =~ /\.([^\.]+)$/;

Re: Extracting just the extension from a filename.
by thor (Priest) on Jul 20, 2002 at 16:23 UTC
    In the spirit of TMTOWTDI:
    my $fileName = "blah.csv"; my @array = split '\.', $fileName; print $array[-1],"\n";

    thor

Re: Extracting just the extension from a filename.
by dug (Chaplain) on Jul 19, 2002 at 19:20 UTC
    $string =~ s/.*\.(.*)$/$1/;
    I'm certainly not a regex whiz, but this seems to do what you want. It will handles "."'s within the filename, and gives you the string after the last dot.

    HTH,
    dug
      Why are you capturing the extension if you put it back into the string though? This works the same: $string =~ s/^.*[.]//; But it has a problem: for files without an extension, it will return the full filename. If you want to do it with a subsitution rather than a match, it should be $string =~ s/^.*(?:[.]([^.]*))?$/$2/; This way the dot plus extension becomes optional; the base filename is still always erased. But it's so clumsy.. a match is more elegant: my ($extension) = $string =~ /[.]([^.]*)$/;

      Makeshifts last the longest.

        Why are you capturing the extension if you put it back into the string though?

        Too many spare cycles on my machine? Not enough caffine? <grin> Good point.

        Also, thanks for posting the more elegant solution.
Re: Extracting just the extension from a filename.
by krujos (Curate) on Jul 20, 2002 at 05:29 UTC
    What about $filename=~/\.(\S+)\s/ Just a thought...
      Why the down vote? At least explain what is so horrible about my solution please.
      Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183387]
Approved by IlyaM
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found