Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Path part of pathname\\file

by Anonymous Monk
on Mar 15, 2004 at 04:53 UTC ( [id://336604]=perlquestion: print w/replies, xml ) Need Help??

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

I have a filename including path: "D:\nt\tools\whoa.txt". I can use basename to get the whoa.txt. How can I get the pathname--"D:\nt\tools\"?

Replies are listed 'Best First'.
Re: Path part of pathname\\file
by Koosemose (Pilgrim) on Mar 15, 2004 at 05:05 UTC

    You might wish to examine the documentation for File::Basename, the answer you seek is contained within :)

    Just Another Perl Alchemist
      You might wish to examine the documentation for File::Basename, the answer you seek is contained within :)
      ... and especially the hidden treasure dirname() thou shalt devote your attention to!

      --saintmike

Re: Path part of pathname\\file
by etcshadow (Priest) on Mar 15, 2004 at 05:14 UTC
    Check out the File::Spec module. Specifically the splitpath method:
    splitpath Splits a path in to volume, directory, and filename portions. On systems with no concept of volume, returns '' for volume. ($volume,$directories,$file) = File::Spec->splitpath( $pa +th ); ($volume,$directories,$file) = File::Spec->splitpath( $pa +th, $no_file ); For systems with no syntax differentiating filenames from directories, assumes that the last file is a path unless $no_file is true or a trailing separator or /. or /.. is present. On Unix this means that $no_file true makes this return ( '', $path, '' ). The directory portion may or may not be returned with a trailing '/'. The results can be passed to the catpath() entry elsewhere in this documentto get back a path equivalent to (usually identical to) the original path.
    ------------ :Wq Not an editor command: Wq
Re: Path part of pathname\\file
by esskar (Deacon) on Mar 15, 2004 at 07:49 UTC
    or doinig it on your own
    my $path = "D:\\nt\\tools\\whoa.txt"; my ($pathname, $filename) = splitpath($path); print "$pathname => $filename\n"; sub splitpath { my ($path) = @_; my $pos = rindex($path, "\\"); return ("", "") if $pos < 0; my $filename = substr($path, $pos+1); my $pathname = substr($path, 0, $pos+1); return ($pathname, $filename); }
Re: Path part of pathname\\file
by z3d (Scribe) on Mar 15, 2004 at 12:21 UTC
    Naivete shining through this morning, but if you can already pull out the file name, why not just get the pathname from what remains? (assuming you are trying to keep your exposure to modules and classes at a minimum ;). For instance, $path =~ s/$file$//;, though ugly and probably incredibly poor coding (I freely admit) is just as quick I would think at this point (assuming $path is your original path w/file name, and $file is your pulled out filename).



    "I have never written bad code. There are merely unanticipated features."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-18 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found