drewrockshard
asked on
Get Filename From Windows Path without using external Perl Modules
How would I go about getting the filename from any type of windows pathname, using strictly the core perl modules? Here's my string:
C:\Users\Drew\Desktop\Dad\ tiffs\file name.tif
I need to get just the filename (with extension), and the pathname may change at any given time, so I need some sort of regex or some type of way to JUST get the filename, no matter how the path looks.
Thanks in advanced.
C:\Users\Drew\Desktop\Dad\
I need to get just the filename (with extension), and the pathname may change at any given time, so I need some sort of regex or some type of way to JUST get the filename, no matter how the path looks.
Thanks in advanced.
You could also use the split function, and take the last output from it.
my $PathName = 'C:\Users\Drew\Desktop\Dad\tiffs\filename.tif'; #This can be whatever
my $FileNameWithExtension = (split(/[\\\/]+/, $PathName))[-1]; #This will get the file name with extension
There is also File::Basename
use File::Basename;
($name,$path,$suffix) = fileparse($fullname,@suffixlist);
$name = fileparse($fullname,@suffixlist);
$basename = basename($fullname,@suffixlist);
$dirname = dirname($fullname);
This will give you the full file name ($fiel), the name without the extension ($name), and the extension ($ext). The last line, "print..." is just there to show it works.
I used the pipe as the beginning and ending of the regex so that it could easily be changed to use a Unix path. Just change \\ to /
When it is for Windows, the back slash has to be escaped, since the back slash IS the escape character. Without escaping it, the back slash would escape the open paren after it.
I used the pipe as the beginning and ending of the regex so that it could easily be changed to use a Unix path. Just change \\ to /
When it is for Windows, the back slash has to be escaped, since the back slash IS the escape character. Without escaping it, the back slash would escape the open paren after it.
my $path = 'C:\Users\Drew\Desktop\Dad\tiffs\filename.tif';
$path =~ m|.*\\((.*?)\.(.*))|;
my ($file,$name,$ext) = ($1,$2,$3);
print "fullname: $file, without ext: $name, ext: $ext\n";
Output:
fullname: filename.tif, without ext: filename, ext: tif
Obviously, there is a typo in my first line - should be $file, not $fiel. Also, further explanation. The reason the regex takes only what is after the last back slash is because it starts with .*\\ which means to be "greedy" in regex speak. If it had .*?\\ that would go only up the the first back slash, or be "non-greedy." The outside parens get everything after the last back slash, the first inside parens get everything from the last back slash to the first period, and the second inside parens get everything after the first period.
If there is a possibility that the flie name could have more than one period, like "this.here.file.tif" then use this, which gets everything to the last period.
If there is a possibility that the flie name could have more than one period, like "this.here.file.tif" then use this, which gets everything to the last period.
my $path = 'C:\Users\Drew\Desktop\Dad\tiffs\this.here.file.tif';
$path =~ m|.*\\((.*)\.(.*))|;
my ($file,$name,$ext) = ($1,$2,$3);
print "fullname: $file, without ext: $name, ext: $ext\n";
Output:
fullname: this.here.file.tif, without ext: this.here.file, ext: tif
Sorry for a third post, but I just re-read your original question. You said you want just the file name WITH the extension. My solutions assumed you might want the extension separately.
my $path = 'C:\Users\Drew\Desktop\Dad\tiffs\filename.tif';
$path =~ m|.*\\(.*)|;
my $filename = $1;
fredmc> This will not work if there is no directory component to the path. eg:
$path = 'filename.tif';
I would expect that $filename in this case would be equal to $path.
$path = 'filename.tif';
I would expect that $filename in this case would be equal to $path.
Adam314 > You are correct. The following will work for both. Group the first bit, and use ?: so it doesn't put it into $1. Add a * after it, which allows for nothing there.
my $path = 'filename.tif';
$path =~ m|(?:.*\\)*(.*)|;
my $filename = $1;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Note that with your code, if the string does not match the pattern, $fpath and $fn will not be set - they will be left at whatever value they were previously.
ASKER
Understandable, however, this will always match due to the fact that there is additional code that I did not post that get's a list of all files, so therefore it will match everytime. Only tiff files are grabbed, so there is no issue there.
http://search.cpan.org/~smueller/PathTools-3.31/lib/File/Spec.pm
See the splitpath function