Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: parse file name

by bichonfrise74 (Vicar)
on Sep 23, 2009 at 21:43 UTC ( [id://797082]=note: print w/replies, xml ) Need Help??


in reply to parse file name

You can simply use split to get the filename.
#!/usr/bin/perl use strict; my $long_file = '/abc/xyz/abc.txt'; my $file = ( split( "/", $long_file ) )[-1]; print "$file\n";

Replies are listed 'Best First'.
Re^2: parse file name
by Anonymous Monk on Sep 24, 2009 at 16:50 UTC
    The scenario is, I need to get the filename and file path no matter it is in windows or unix type.

    it can be either of the below
    my $long_file = '/abc/xyz/abc.txt';
    my $long_file = '\abc\xyz\abc.txt';

    In this case File::Basename and File::Spec wont return the directory part if the file name is windows format and i am running the script from unix.

    Any Solution for this?
    Thanks, Tom

      Replace all backslashes in $long_file with forward slashes before working with $long_file. Using the tr/// operator should be the most efficient implementation.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://797082]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found