Link to home
Start Free TrialLog in
Avatar of drewrockshard
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\filename.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.
Avatar of FishMonger
FishMonger
Flag of United States of America image

You could use a simple regex, but I think it would be best to use File::Spec which is a core module.

http://search.cpan.org/~smueller/PathTools-3.31/lib/File/Spec.pm

See the splitpath function
Avatar of Adam314
Adam314

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

Open in new window

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);

Open in new window

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.
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

Open in new window

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.
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

Open in new window

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;

Open in new window

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.
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;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of drewrockshard
drewrockshard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of drewrockshard

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.