Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

split a path/filename

by akrrs7 (Acolyte)
on Dec 01, 2011 at 17:28 UTC ( [id://941124]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have a file.txt that has a list of path/filenames like:

a/b/c/filename1.someext

b/c/filename2.sh

d/e/f/g/filename3.exe

I need to read that file and split each line it into a path and a filename. This is my code so far:
#$ps is my path separator - could be \ or / open theInputFile, $inputFile or die "Could not read from $inputFile, +program halting. \n"; while (<theInputFile>) { chomp; my $fileBeingChecked = "$_"; my $fileName = (split /\${ps}/,$fileBeingChecked)[-1]; my $path = NOT SURE WHAT TO DO HERE

Replies are listed 'Best First'.
Re: split a path/filename
by toolic (Bishop) on Dec 01, 2011 at 17:38 UTC
    File::Basename
    use warnings; use strict; use File::Basename; while (<DATA>) { chomp; my $basename = basename($_); my $dirname = dirname ($_); print "$dirname ... $basename\n"; } __DATA__ a/b/c/filename1.someext b/c/filename2.sh d/e/f/g/filename3.exe
      From the same module:
      my ($basename, $dirname) = fileparse($_);
Re: split a path/filename
by Anonymous Monk on Dec 01, 2011 at 17:51 UTC
    Path::Class
    #!/usr/bin/perl -- use strict; use warnings; use Path::Class; while (<DATA>) { chomp; my $file = file( $_ ); print $file->parent, ' ... ', $file->basename, "\n"; } __DATA__ a/b/c/filename1.someext b/c/filename2.sh d/e/f/g/filename3.exe
Re: split a path/filename
by dd-b (Monk) on Dec 01, 2011 at 18:38 UTC
    And I'm in the habit of using File::Spec. So now I've learned of two other approaches.

      File::Spec is hard to use. I'm not even sure if the following is correct.

      use File::Spec::Functions qw( catpath curdir splitpath ); my ($volume, $directories, $basename) = splitpath($qfn); $directories = curdir() if !length($directories); my $dirname = catpath($value, $directories, ''); say $dirname; say $basename;

      Path::Class is a wrapper around it.

      use Path::Class qw( file ); my $file = file($qfn); my $basename = $file->basename(); my $dirname = $file->parent(); say $dirname; say $basename;
        File::Spec is hard to use.

        Even worse, it is broken.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: split a path/filename
by TJPride (Pilgrim) on Dec 02, 2011 at 03:54 UTC
    use strict; use warnings; my ($path, $file); while (<DATA>) { chomp; ($path, $file) = splitPath($_); print "My path is $path\n"; print "My file is $file\n\n"; } sub splitPath { if ($_[0] =~ m|(.*/)(.*)|) { return ($1, $2); } return ('', $_[0]); } __DATA__ a/b/c/filename1.someext b/c/filename2.sh d/e/f/g/filename3.exe filename.txt

      What makes you think that every operating system uses the slash as directory separator?

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Obviously, not all do, but in this case he already specified his path format, and this sounds like a quick hack conversion, not a robust app. Enough people already suggested modules anyway.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found