Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I extract a file name from a path string

( #35235=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Oct 04, 2000 at 12:28 UTC
Q&A  > strings


Description:

I am using:
open FILES, "dir /b /aa /s |" or die; while(<FILES>) { #process result } close FILES;
On NT this gives me a list like:
C:\WINDOWS\INF\wordpad.inf C:\WINDOWS\INF\wordpad.PNF C:\WINDOWS\INF\WPIE3x86.INF C:\WINDOWS\INF\WPIE3x86.PNF . . .
How do I split this string into two parts, the directory path and the filename?

Answer: How do I extract a file name from a path string
contributed by t0mas

The File::Basename module is probably what you want.

use File::Basename; ( $name, $path, $suffix ) = fileparse( $filename_with_path, "\.[^.]*" +);
/brother t0mas
Answer: How do I extract a file name from a path string
contributed by knight

The File::Spec module will let you separate the volume, without having to worry about the suffix:

use File::Spec; ( $volume, $directories, $file ) = File::Spec->splitpath( $path );
Answer: How do I extract a file name from a path string
contributed by Fastolfe

You should consider solving your problem in a different way. By using the opendir and readdir functions to get the entries in a directory, you automatically have the filename separate from the directory name, and you can do it all without spawning an external process.

Answer: How do I extract a file name from a path string
contributed by vaevictus

open FILES, "dir /b /aa /s |" or die; while ( <FILES> ) { my( $dir, $file ) = /(.+)\\(.+)/; defined $file or next; # no path separator print "$dir => $file\n"; } close FILES;
Answer: How do I extract a file name from a path string
contributed by amasidlover

This works for both DOS and UNIX path separators and when there is no path info.

( $dir, $file ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ );

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (13)
    As of 2015-07-06 19:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (81 votes), past polls