Re: How do I extract a file name from a path string⭐
by t0mas (Priest) on Oct 04, 2000 at 13:21 UTC
|
use File::Basename;
( $name, $path, $suffix ) = fileparse( $filename_with_path, "\.[^.]*"
+);
/brother t0mas | [reply] [Watch: Dir/Any] [d/l] |
Re: How do I extract a file name from a path string⭐
by knight (Friar) on Oct 04, 2000 at 22:31 UTC
|
The File::Spec module will let you separate the volume, without having to
worry about the suffix:
use File::Spec;
( $volume, $directories, $file ) = File::Spec->splitpath( $path );
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I extract a file name from a path string
by Fastolfe (Vicar) on Oct 04, 2000 at 21:04 UTC
|
You should consider solving your problem in a different way.
By using the opendir and readdir functions to get the entries in a directory,
you automatically have the filename separate from the directory name, and you can do it
all without spawning an external process. | [reply] [Watch: Dir/Any] |
Re: How do I extract a file name from a path string
by gar (Initiate) on Feb 26, 2008 at 03:40 UTC
|
i loved amasidlover's answer, but i noticed that it needs one small revision to the regexp.
instead of:
m/^.+[\\|\/](.+?)$/
use:
m/^.*[\\|\/](.+?)$/
If you don't, then a path like '/file.txt' will show up as '/file.txt' instead of 'file.txt'.
not claiming i know 'why' either of them work, but they just do.
Like, I can't figure out how, when you have only a
filename without a path or slash, that the first bit ends up
not failing the expression or ending up as part of the (captured) $1 expression.
Originally posted as a Categorized Answer. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How do I extract a file name from a path string
by vaevictus (Pilgrim) on Oct 06, 2000 at 18:34 UTC
|
open FILES, "dir /b /aa /s |" or die;
while ( <FILES> )
{
my( $dir, $file ) = /(.+)\\(.+)/;
defined $file or next; # no path separator
print "$dir => $file\n";
}
close FILES;
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I extract a file name from a path string
by Anonymous Monk on Oct 08, 2004 at 15:00 UTC
|
##Typo in the previous answer
$data[$#data]
Originally posted as a Categorized Answer. | [reply] [Watch: Dir/Any] [d/l] |
Re: How do I extract a file name from a path string
by amasidlover (Sexton) on Jan 17, 2005 at 14:50 UTC
|
This works for both DOS and UNIX path separators and when there is no path info.
( $dir, $file ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ );
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
use File::Spec::Functions;
my $fname = '/home/legato/filename.pl';
my ($vol, $path, $filename) = splitpath($fname);
# $filename now contains 'filename.pl'
A pattern match will not be as platform-aware (or as readable, incidentally) or as thorough as the above code, which is not significantly longer.
Anima Legato .oO all things connect through the motion of the mind
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I extract a file name from a path string
by Anonymous Monk on Oct 08, 2004 at 14:58 UTC
|
$filepath = '/u/test/global/filename.txt';
my ( @data ) = split /\//, $filepath;
my ( $filename ) = $data[$#data];
print "$filename" ; ## will print filename.txt
| [reply] [Watch: Dir/Any] [d/l] |
|
my $filepath = '/u/test/global/filename.txt';
my $fname = substr($filepath, rindex($filepath, '/') + 1);
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] |