CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
string operation to get the file name

 



DoolinDalton
Novice

Sep 28, 2009, 11:00 AM

Post #1 of 8 (922 views)
string operation to get the file name Can't Post

I have a string that represents the full path for a file, ie, something like,

c:\folder\sub_folder\...\last_sub_folder\filename.txt

There is an unknown level of sub folders, and I'm trying to substring out only the file name portion of the string. (without the .txt or whatever extention I may find)

I see this as a two part substring, once to cut out the path, and again to cut out the file extension. But if I were to work with the string as-is, I think I need to use regular expressions, since I don't know how deep the subfolders are... I need to somehow pick out the position of the last "\".

But, if I can somehow reverse the original string, then I can think of an easy way to do this... first cut out the file extension by looking for the first ".", then cut out the path by looking for the first "\" and re-reverse the result... But I don't see a reverse function for a string.

Hopefully, you followed what I said.


EDIT: or is there a way to glob without getting the entire path? That would avoid having to do the above.


(This post was edited by DoolinDalton on Sep 28, 2009, 11:06 AM)


savo
User

Sep 28, 2009, 11:10 AM

Post #2 of 8 (917 views)
Re: [DoolinDalton] string operation to get the file name [In reply to] Can't Post

reverse can be done and is shown below but i think you want File::Basename

taken from learning perl


Code
@backwards = reverse qw/ yabba dabba doo /; 
# gives doo, dabba, yabba
$backwards = reverse qw/ yabba dabba doo /;
# gives oodabbadabbay



DoolinDalton
Novice

Sep 28, 2009, 11:15 AM

Post #3 of 8 (914 views)
Re: [savo] string operation to get the file name [In reply to] Can't Post

I thought reserve() function was to reverse the elements of an array, but I guess it works with strings also. And what id File::Basename?


savo
User

Sep 28, 2009, 11:21 AM

Post #4 of 8 (912 views)
Re: [DoolinDalton] string operation to get the file name [In reply to] Can't Post

It can pick out the path and the command for you. I haven't used it just read about it i think it may do what you need.

http://perldoc.perl.org/File/Basename.html


FishMonger
Veteran / Moderator

Sep 28, 2009, 11:23 AM

Post #5 of 8 (911 views)
Re: [DoolinDalton] string operation to get the file name [In reply to] Can't Post

http://search.cpan.org/~dapm/perl-5.10.1/lib/File/Basename.pm

How are you assigning the var that holds the path?

Do you need to iterate over the files within 1 or more directories?

Besides the glob function, you can use the opendir/readdir functions which returns each directory item without its path.


DoolinDalton
Novice

Sep 28, 2009, 11:48 AM

Post #6 of 8 (908 views)
Re: [FishMonger] string operation to get the file name [In reply to] Can't Post

Thank you for the responses. I will read about Basename. But now that I see that I can reverse the entire path-filename string, I can use substr() combined with index() to do what I need.

To answer someone from above, I have an array that was initialized using glob(). Now I am going thru this array and doing some cleanup.


FishMonger
Veteran / Moderator

Sep 28, 2009, 12:15 PM

Post #7 of 8 (903 views)
Re: [DoolinDalton] string operation to get the file name [In reply to] Can't Post


In Reply To
Thank you for the responses. I will read about Basename. But now that I see that I can reverse the entire path-filename string, I can use substr() combined with index() to do what I need.


Why reinvent the wheel?


Code
use strict; 
use warnings;
use File::Basename;
use Data::Dumper;

my $path = 'c:\folder\sub_folder\...\last_sub_folder\filename.txt';

my($filename, $directories, $suffix) = fileparse($path, qr/\.[^.]+$/);

print Dumper $filename, $directories, $suffix;



Quote
I:\>test.pl
$VAR1 = 'filename';
$VAR2 = 'c:\\folder\\sub_folder\\...\\last_sub_folder\\';
$VAR3 = '.txt';



shawnhcorey
Enthusiast


Sep 28, 2009, 4:35 PM

Post #8 of 8 (896 views)
Re: [DoolinDalton] string operation to get the file name [In reply to] Can't Post


In Reply To
Thank you for the responses. I will read about Basename. But now that I see that I can reverse the entire path-filename string, I can use substr() combined with index() to do what I need.


I agree with FishMonger; don't re-invent the wheel. Also, if you use the tools designed to manipulate file names and directory paths, you have portability. And not just portability of the code, portability of your skill sets. Once you learn to use them, you know you can use them anywhere regardless of what OS you find yourself working on in the future.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives