For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > October 2004 > File extension extraction from filename









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author File extension extraction from filename
Deke

2004-10-12, 8:55 pm

How does one extract check for the file extension part or a filename in
Perl?

Thanks
A. Sinan Unur

2004-10-12, 8:55 pm

Deke <Deke@nospam.com> wrote in news:10mojl1hbb74va0@news.supernews.com:

> How does one extract check for the file extension part or a filename in
> Perl?


I assume the filenames you are interested in are in the following format:

name.extension

That is, I am going to require that the extension of

..bashrc

is blank.

We are interested in the string following the last period in the name:

#! /usr/bin/perl

use strict;
use warnings;

my @list = qw(name name.extension .extension);

for (@list) {
my ($ext) = /.+\.(\w+)?$/;
defined $ext or $ext = '';

print "Name: $_\tExtension: $ext\n";
}

__END__

Also, look into File::Basename::fileparse

http://search.cpan.org/~nwclark/per...ile/Basename.pm

Sinan.
Jürgen Exner

2004-10-13, 3:55 am

Deke wrote:
> How does one extract check for the file extension part or a filename
> in Perl?


Any particular problems with File::Basename?

jue


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com