[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Get Filename From Windows Path without using external Perl Modules

Posted on 2010-03-30
11
Medium Priority
?
545 Views
Last Modified: 2012-05-09
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.
0
Comment
Question by:drewrockshard
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 29146895
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
 
LVL 39

Expert Comment

by:Adam314
ID: 29163527
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
 
LVL 6

Expert Comment

by:tty2
ID: 29164661
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
LVL 5

Expert Comment

by:fredmc
ID: 29214540
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
 
LVL 5

Expert Comment

by:fredmc
ID: 29215786
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
 
LVL 5

Expert Comment

by:fredmc
ID: 29216216
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
 
LVL 39

Expert Comment

by:Adam314
ID: 29222251
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
 
LVL 5

Expert Comment

by:fredmc
ID: 29225654
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
 
LVL 2

Accepted Solution

by:
drewrockshard earned 0 total points
ID: 29237381
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
 
LVL 39

Expert Comment

by:Adam314
ID: 29249786
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
 
LVL 2

Author Comment

by:drewrockshard
ID: 29266773
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Six Sigma Control Plans
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question