Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

extract file name from a dynamic path

by dani_cv (Acolyte)
on Sep 06, 2008 at 17:08 UTC ( [id://709527]=perlquestion: print w/replies, xml ) Need Help??

dani_cv has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,
$tst = 'test\testweb\testlibb\file.-finaltestfla.jar';
I need to extract the 'file.-finaltestfla.jar' from this.
The path of the depth may be any long, but the rule that I am looking for is to extract the last the string (fle name) after the last '\'

Any help would be great.
Thanks in advance.

Replies are listed 'Best First'.
Re: extract file name from a dynamic path
by ikegami (Patriarch) on Sep 06, 2008 at 17:13 UTC

      While writing my last CUFP I found a warning about basename in File::Basename:

      This function is provided for compatibility with the Unix shell command basename(1). It does NOT always return the file name portion of a path as you might expect. To be safe, if you want the file name portion of a path use fileparse().

      I'd like to see an example of this, since I can't figure out what can be wrong with basename.

        Just keep reading, it says
        C<basename()> returns the last level of a filepath even if the last level is clearly directory. In effect, it is acting like C<pop()> for paths. This differs from C<fileparse()>'s behaviour. # Both return "bar" basename("/foo/bar"); basename("/foo/bar/");
Re: extract file name from a dynamic path
by johngg (Canon) on Sep 06, 2008 at 17:15 UTC
Re: extract file name from a dynamic path
by harleypig (Monk) on Sep 06, 2008 at 17:34 UTC

    I would agree with the response above. Use File::Basename. It handles a lot of things you wouldn't necessarily think to include.

    However, for the sake of completeness, there are a couple of ways you could do this:

    ( $filename ) = $tst =~ /^.*\\(.*)$/

    $filename = ( split /\\/, $tst )[-1]

    Harley J Pig
Re: extract file name from a dynamic path
by SFLEX (Chaplain) on Sep 06, 2008 at 18:07 UTC
    You could use a module or write the 1 line of code to do the job.
    Here's another way.
    my $file_name = ''; $tst = 'test\testweb\testlibb\file.-finaltestfla.jar'; $file_name = $1 if ($tst =~ m/\A\S+\\(.*?\.jar)\z/i); print $file_name . "\n" . $tst;
      john@perl:~$ perl -le' my $file_name = q[]; $tst = q[test\test web\test libb\file.-finaltestfla.jar]; $file_name = $1 if ($tst =~ m/\A\S+\\(.*?\.jar)\z/i); print $file_name . "\n" . $tst; ' test web\test libb\file.-finaltestfla.jar test\test web\test libb\file.-finaltestfla.jar
        Thank you all!
        the original path didn't have spaces

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://709527]
Approved by ikegami
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-07-20 10:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.