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.
LVL 2
drewrockshardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
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
0
Adam314Commented:
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

0
tty2Commented:
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

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Frederick McIntyreProgrammerCommented:
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

0
Frederick McIntyreProgrammerCommented:
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

0
Frederick McIntyreProgrammerCommented:
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

0
Adam314Commented:
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.
0
Frederick McIntyreProgrammerCommented:
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

0
drewrockshardAuthor Commented:
Guys,

I actually went another route.  I ended up using RegEx to get both the filename and the pathname, so that I could later manipulate the data as I wanted to.
if (/^(..[^:]*?\\)([^:\\]*\.tiff?):/im) {
$fpath = $1;
$fn = $2;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Adam314Commented:
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.
0
drewrockshardAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.