469,291 Members | 1,837 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,291 developers. It's quick & easy.

Removing directory from a file path

2
In reference to: How to remove a variable length word from a string

I'm trying to use the code you suggested to shalini for extracting a substring from a string...but not able to do so successfully..pls help

This is what i want to do:

$string='/abc/xyz/log/tftskmst.user00001.LOG' ;

Now i want to remove "/apps/prodcopy/log/" from this string. The length of this part of the string may vary that is it may be "/abc/xyz/log/" or "/abc/xyz/log/20070613.144401/".

The key in this string i want is "user" which will always be there in the name of the log files .
and i also want both the part of string to be stored in some variable

or

is there some way in which we can find the last occurence of "/" and then take the part before and after "/" into variables
Jul 8 '07 #1
8 9323
svarc
2
I'm trying to write a code for extracting a substring from a string...but not able to do so successfully..pls help

This is what i want to do:

$string='/abc/xyz/log/tftskmst.user00001.LOG' ;

Now i want to remove "/abc/xyz/log/" from this string. The length of this part of the string may vary that is it may be "/abc/xyz/log/" or "/abc/xyz/log/20070613.144401/".

The key in this string i want is "user" which will always be there in the name of the log files .
and i also want both the part of string to be stored in some variable

right now i m using the following code
my $string = '/abc/xyz/log/tftskmst.user00001.LOG';
my $check = 'tfts';

my $index = index $string, $check;

my $address = substr $string, 0, $index;
my $filename= substr $string, $index;

print "$address\n";
print "$filename\n";

but i dont want to hardcode the check part...please suggest

or

is there some way in which we can find the last occurence of "/" and then take the part before and after "/" into variables
Jul 8 '07 #2
numberwhun
3,503 Expert Mod 2GB
Well, to me, this looks like you want to obtain the "basename" of an absolute path to a file. Here is how I would do this:

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2.  
  3. my $string = '/abc/xyz/log/tftskmst.user00001.LOG';
  4. my($filename, $directories, $suffix) = fileparse($string);
  5.  
  6. print("$filename");
  7.  
Let us know if that does what you want.

Regards,

Jeff
Jul 8 '07 #3
miller
1,089 Expert 1GB
Use File::Basename

perldoc File::Basename

Expand|Select|Wrap|Line Numbers
  1. use File::Basename qw(fileparse);
  2. my $string='/abc/xyz/log/tftskmst.user00001.LOG' ;
  3. my ($file, $path) = fileparse($string);
  4. print "$file\n";
  5.  
- Miller
Jul 8 '07 #4
numberwhun
3,503 Expert Mod 2GB
Use File::Basename

perldoc File::Basename

Expand|Select|Wrap|Line Numbers
  1. use File::Basename qw(fileparse);
  2. my $string='/abc/xyz/log/tftskmst.user00001.LOG' ;
  3. my ($file, $path) = fileparse($string);
  4. print "$file\n";
  5.  
- Miller
Actually, the person you replied to posted this same question in a seperate thread(which I coincidently replied to with the same answer you gave). I really wish that people would stop creating two copies of their questions. One copy at the end of someone else's thread and then their own new thread.

If you are going to ask a question that differs from someone else's question, then ask it, but in a new thread, not in the same thread that you just read. If you question is a carry-on continuation of the same topic, fine, but don't double post, please! I have seen far too much of that in the past couple of weeks and I am sure others are tired of it, especially our wonderful moderators who have to even things out. ;-)

Regards,

Jeff
Jul 8 '07 #5
KevinADC
4,059 Expert 2GB
I also recommend you use the module/code Jeff posted. You could also have done this using split() or a regexp or even rindex() and substr().

In the future do not post with "urgent!!!!" (or other useless exclamation) as part of your subject. Your question will be answered the same as all other questions.

Thanks,
Kevin
Jul 8 '07 #6
miller
1,089 Expert 1GB
Greetings Jeff,

I normally would have noticed this type of thing before responding, but I was in a rush this morning. I'll merge the threads now, but as you say, this is just something that happens occasionally. I don't really expect people who searched for a solution to know the difference between creating a new thread with a reference versus simply replying. They are often new posters, and there isn't an interface to do such a thing right now easily. So the quickest solution for now is to just solve it from the moderator perspective. Off to do that now.

- Miller
Jul 9 '07 #7
miller
1,089 Expert 1GB
Threads merged and given a more appropriate title.

- MODERATOR
Jul 9 '07 #8
numberwhun
3,503 Expert Mod 2GB
Greetings Jeff,

I normally would have noticed this type of thing before responding, but I was in a rush this morning. I'll merge the threads now, but as you say, this is just something that happens occasionally. I don't really expect people who searched for a solution to know the difference between creating a new thread with a reference versus simply replying. They are often new posters, and there isn't an interface to do such a thing right now easily. So the quickest solution for now is to just solve it from the moderator perspective. Off to do that now.
Well, one reason you probably keep so busy is all the double postings. ;-) I didn't mean to rant so awefully, but it was just getting annoying, that's all. Thanks for your dilligence in moderating!

Regards,

Jeff
Jul 9 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by hokiegal99 | last post: by
6 posts views Thread by bart_nessux | last post: by
8 posts views Thread by James Owens | last post: by
4 posts views Thread by Elmo Watson | last post: by
5 posts views Thread by rbt | last post: by
reply views Thread by nhmark64 | last post: by
9 posts views Thread by David Pratt | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.