For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2007 > Removing file extension









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 Removing file extension
Saravana Kumar

2007-01-23, 3:59 am

Hi list,

I am trying to remove the extension from the a list of filenames and
manipulate the names further.

Tried to doing this:
$file=~ s/\..*//;

The above works fine. I get the result 'filename' if the filename is
filename.ext.

There are some files whose names are like file.name.ext and the result i get
for this is 'file' while the desired result is 'file.name'. Is there a way
to fix this?

TIA,
SK

shaick mohamed

2007-01-23, 3:59 am

Try this
s/(.*)\..*/\1/;

Thanks,
Shaick.

On 1/23/07, Saravana Kumar <tuxkumar@gmail.com> wrote:
> Hi list,
>
> I am trying to remove the extension from the a list of filenames and
> manipulate the names further.
>
> Tried to doing this:
> $file=~ s/\..*//;
>
> The above works fine. I get the result 'filename' if the filename is
> filename.ext.
>
> There are some files whose names are like file.name.ext and the result i get
> for this is 'file' while the desired result is 'file.name'. Is there a way
> to fix this?
>
> TIA,
> SK
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

Saravana Kumar

2007-01-23, 3:59 am

shaick mohamed wrote:
[color=darkred]
> Try this
> s/(.*)\..*/\1/;
>
> Thanks,
> Shaick.
>
> On 1/23/07, Saravana Kumar <tuxkumar@gmail.com> wrote:
I got this warning:
\1 better written as $1

But the result was what i expected(file.name)

Thanks for your help.

---
SK

Xavier Noria

2007-01-23, 7:58 am

On Jan 23, 2007, at 8:22 AM, Saravana Kumar wrote:

> Hi list,
>
> I am trying to remove the extension from the a list of filenames and
> manipulate the names further.
>
> Tried to doing this:
> $file=~ s/\..*//;
>
> The above works fine. I get the result 'filename' if the filename is
> filename.ext.
>
> There are some files whose names are like file.name.ext and the
> result i get
> for this is 'file' while the desired result is 'file.name'. Is
> there a way
> to fix this?


Yes, you need to assert in the regexp dots are not allowed in the
extension, for example something like this:

$file =~ s/\.\w+$//;

Note that no captures are needed.

-- fxn

Rob Dixon

2007-01-23, 7:58 am

Saravana Kumar wrote:
>
> shaick mohamed wrote:
>
> I got this warning:
> \1 better written as $1
>
> But the result was what i expected(file.name)


(Please bottom-post all responses. Thank you.)

$file =~ s/(.*)\./$1/;

or

$file =~ s/\.[^.]*$//;

Rob
Igor Sutton Lopes

2007-01-23, 7:58 am


On 2007/01/23, at 11:03, Rob Dixon wrote:

> $file =~ s/(.*)\./$1/;
>
> or
>
> $file =~ s/\.[^.]*$//;
>


If you know the suffix of the files you're working on, you can use
the File::Basename module, more specific the fileparse function:

use File::Basename;

my @suffix = qw(.txt .zip .doc);
my $filepath = "/tmp/something.txt";
my ($name, $path, $suffix) = fileparse($filepath, @suffix);

HTH!

--
Igor Sutton
igor.sutton@gmail.com




kens

2007-01-23, 7:58 am


Igor Sutton Lopes wrote:
> On 2007/01/23, at 11:03, Rob Dixon wrote:
>
>
> If you know the suffix of the files you're working on, you can use
> the File::Basename module, more specific the fileparse function:
>
> use File::Basename;
>
> my @suffix = qw(.txt .zip .doc);
> my $filepath = "/tmp/something.txt";
> my ($name, $path, $suffix) = fileparse($filepath, @suffix);
>
> HTH!
>
> --
> Igor Sutton
> igor.sutton@gmail.com
>
>


Actually, you do not need to know the suffix:

my ($name, $path, $suffix) = fileparse($filepath, '\..*');

Ken

Purl Gurl

2007-01-23, 7:04 pm

Saravana Kumar wrote:

> I am trying to remove the extension from the a list of filenames and
> manipulate the names further.


> There are some files whose names are like file.name.ext and the result i get
> for this is 'file' while the desired result is 'file.name'.


#!perl

$filename = "file.ext.txt";

print substr ($filename, 0, rindex($filename, "."));

(fails if no extension)

--

#!perl

$filename = "file.ext.txt";

if (index ($filename, ".") > -1)
{ print substr ($filename, 0, rindex($filename, ".")); }
else
{ print "Your File Has No Extension: $filename"; }

(no return if extension only)

--

#!perl

$filename = ".htaccess";

if (index ($filename, ".") == 0)
{ print "Special Case Filename: $filename"; }
elsif (index ($filename, ".") > 0)
{ print substr ($filename, 0, rindex($filename, ".")); }
else
{ print "Your File Has No Extension: $filename"; }

(does not note multiple extension)

--

#!perl

$filename = "file.ext";

if (index ($filename, ".") == 0)
{ print "Special Case Filename: $filename"; }
elsif ($filename =~ tr/././ > 1)
{ print "Multiple Extension ($filename): ", substr ($filename, 0, rindex($filename, ".")); }
elsif (index ($filename, ".") > 0)
{ print "Single Extension ($filename): ", substr ($filename, 0, rindex($filename, ".")); }
else
{ print "Your File Has No Extension: $filename"; }

--

Purl Gurl


Purl Gurl

2007-01-23, 7:04 pm

kens wrote:

> Igor Sutton Lopes wrote:
[color=darkred]
[color=darkred]
[color=darkred]
> Actually, you do not need to know the suffix:


> my ($name, $path, $suffix) = fileparse($filepath, '\..*');



Do not declare "my" lexical variables on a global basis. This
only serves to decrease script efficiency.

#!perl

use File::Basename;

$filepath = "c:/apache/htdocs/.htaccess";

($name, $path, $suffix) = fileparse($filepath, '\..*');

if (!($name))
{ print "FUBAR"; }
else
{ print $name; }


PRINTED RESULTS:

FUBAR


Purl Gurl

John W. Krahn

2007-01-23, 7:04 pm

Igor Sutton Lopes wrote:
>
> On 2007/01/23, at 11:03, Rob Dixon wrote:
>
>
> If you know the suffix of the files you're working on, you can use the
> File::Basename module, more specific the fileparse function:
>
> use File::Basename;
>
> my @suffix = qw(.txt .zip .doc);
> my $filepath = "/tmp/something.txt";
> my ($name, $path, $suffix) = fileparse($filepath, @suffix);


You don't need to know the "suffix" name(s), just use a regular expression:

my $filepath = '/tmp/something.txt';
my ( $name, $path, $suffix ) = fileparse( $filepath, qr/\.[^.]*/ );



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Dr.Ruud

2007-01-24, 4:04 am

Saravana Kumar schreef:

> I am trying to remove the extension from the a list of filenames and
> manipulate the names further.
>
> Tried to doing this:
> $file=~ s/\..*//;
>
> The above works fine. I get the result 'filename' if the filename is
> filename.ext.
>
> There are some files whose names are like file.name.ext and the
> result i get for this is 'file' while the desired result is
> 'file.name'. Is there a way to fix this?
>
> TIA,
> SK


Use an anchor and a negated character set:

s/\.[^.]*$//

--
Affijn, Ruud

"Gewoon is een tijger."
Xavier Noria

2007-01-24, 4:04 am

On Jan 23, 2007, at 9:55 AM, Dr.Ruud wrote:

> Use an anchor and a negated character set:
>
> s/\.[^.]*$//


That solution is broken, think "/foo/bar.baz/woo". Correct regexps
and other approaches have already been posted.

-- fxn

Dr.Ruud

2007-01-24, 8:02 am

Xavier Noria schreef:

> $file =~ s/\.\w+$//;


That solution is broken, think "file.txt~" and "file.$$$", etc.
:)

--
Affijn, Ruud

"Gewoon is een tijger."
Dr.Ruud

2007-01-24, 8:02 am

Xavier Noria schreef:
> On Jan 23, 2007, at 9:55 AM, Dr.Ruud wrote:
[color=darkred]
>
> That solution is broken, think "/foo/bar.baz/woo". Correct regexps
> and other approaches have already been posted.


Paths were not involved.

--
Affijn, Ruud

"Gewoon is een tijger."

Sponsored Links







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

Copyright 2008 codecomments.com