Perl, Removing file from path to get directory only
I need to remove the file name from a path, eg:
$fpath = /home/user/hello/hey.pl So I can make it... $fpath2 = /home/user/hello Instead. Is there an easy way to do this, like some inbuilt function? |
Hi,
Take a look at: File::Basename It can do a lot more then you ask, with that in mind here's a code snippet: Code:
#!/usr/bin/perl |
I love you.
|
*Or* (I don't see much reason to do this but..) you can use the dirname(1) utility.
|
Hi,
True, you could use dirname (1), but it's an external (unix) command, seen from perl that is. dirname [File::Basename] (3) is also part of File::Basename and an internal perl command, which is more resource friendly. Hope this clears things up a bit. |
thanks
Thanks for the above soultion
|
:-)
You are welcome! |
Thank you
Hi! Thank you.. A colleague today asked me how to do this and I directly got the answer when i came here..perfectly worked!
|
The Perl code to do this can be as simple as (if $filename contains fully qualified filespec):
Code:
$filename=~m/^.+\//; --- rod. |
All times are GMT -5. The time now is 04:09 PM. |