Re: parse file name
by Joost (Canon) on Sep 22, 2009 at 17:28 UTC
|
| [reply] |
|
I agree - Since I discovered File::Basename, i use it all the time.
Maybe OP should learn to love Super Search?
Just a something something...
| [reply] |
|
site:perlmonks.org parse file path
Works better for me- searching inside perlmonks.. :-( | [reply] |
|
|
| [reply] |
|
| [reply] |
Re: parse file name
by ccn (Vicar) on Sep 22, 2009 at 17:26 UTC
|
use File::Spec;
my ($volume, $directories, $file) = File::Spec->splitpath( $path );
my ($extension) = $file =~ /\.([^\.]+)$/;
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
Or you could consider it to be a recursive definition, so that .gz is the extension of the perl-5.10.1.tar, and .tar is the extension of perl-5.10.1.
6 of one, 1/2 dozen of another.
--MidLifeXis
Please consider supporting my wife as she walks in the 2009 Alzheimer's Walk.
| [reply] |
Re: parse file name
by leocharre (Priest) on Sep 22, 2009 at 19:57 UTC
|
use File::PathInfo;
my $abs = '/home/myself/thing.txt';
my $o = File::PathInfo->new($abs) or die;
$o->ext;
$o->abs_loc; # then dir it resides in
$o->abs_path;
$o->filename;
$o->filename_only; # filename without ext
| [reply] [d/l] |
Re: parse file name
by sabari (Beadle) on Sep 23, 2009 at 06:34 UTC
|
| [reply] |
Re: parse file name
by bichonfrise74 (Vicar) on Sep 23, 2009 at 21:43 UTC
|
You can simply use split to get the filename.
#!/usr/bin/perl
use strict;
my $long_file = '/abc/xyz/abc.txt';
my $file = ( split( "/", $long_file ) )[-1];
print "$file\n";
| [reply] [d/l] |
|
The scenario is, I need to get the filename and file path no matter it is in windows or unix type.
it can be either of the below
my $long_file = '/abc/xyz/abc.txt';
my $long_file = '\abc\xyz\abc.txt';
In this case File::Basename and File::Spec wont return the directory part if the file name is windows format and i am running the script from unix.
Any Solution for this?
Thanks,
Tom
| [reply] |
|
| [reply] |