http://www.perlmonks.org?node_id=35235

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (strings)

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?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I extract a file name from a path string
by t0mas (Priest) on Oct 04, 2000 at 13:21 UTC
    The File::Basename module is probably what you want.
    use File::Basename; ( $name, $path, $suffix ) = fileparse( $filename_with_path, "\.[^.]*" +);
    /brother t0mas
Re: How do I extract a file name from a path string
by knight (Friar) on Oct 04, 2000 at 22:31 UTC
    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 );
Re: How do I extract a file name from a path string
by Fastolfe (Vicar) on Oct 04, 2000 at 21:04 UTC
    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.
Re: How do I extract a file name from a path string
by gar (Initiate) on Feb 26, 2008 at 03:40 UTC

    i loved amasidlover's answer, but i noticed that it needs one small revision to the regexp.

    instead of:
    m/^.+[\\|\/](.+?)$/
    use:
    m/^.*[\\|\/](.+?)$/

    If you don't, then a path like '/file.txt' will show up as '/file.txt' instead of 'file.txt'.

    not claiming i know 'why' either of them work, but they just do. Like, I can't figure out how, when you have only a filename without a path or slash, that the first bit ends up not failing the expression or ending up as part of the (captured) $1 expression.

    Originally posted as a Categorized Answer.

Re: How do I extract a file name from a path string
by vaevictus (Pilgrim) on Oct 06, 2000 at 18:34 UTC
    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;
Re: How do I extract a file name from a path string
by Anonymous Monk on Oct 08, 2004 at 15:00 UTC
    ##Typo in the previous answer
    $data[$#data]

    Originally posted as a Categorized Answer.

Re: How do I extract a file name from a path string
by amasidlover (Sexton) on Jan 17, 2005 at 14:50 UTC
    This works for both DOS and UNIX path separators and when there is no path info.
    ( $dir, $file ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ );

      I'm not sure why I would want to use that code fragment and not File::Spec::Functions.

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        I wholeheartedly agree:

        use File::Spec::Functions; my $fname = '/home/legato/filename.pl'; my ($vol, $path, $filename) = splitpath($fname); # $filename now contains 'filename.pl'
        A pattern match will not be as platform-aware (or as readable, incidentally) or as thorough as the above code, which is not significantly longer.

        Anima Legato
        .oO all things connect through the motion of the mind

Re: How do I extract a file name from a path string
by Anonymous Monk on Oct 08, 2004 at 14:58 UTC
    $filepath = '/u/test/global/filename.txt'; my ( @data ) = split /\//, $filepath; my ( $filename ) = $data[$#data]; print "$filename" ; ## will print filename.txt
      my $filepath = '/u/test/global/filename.txt'; my $fname = substr($filepath, rindex($filepath, '/') + 1);
        <br>'s removed now.