473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 9824
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,509 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,509 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,509 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

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

Similar topics

9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
6
by: bart_nessux | last post by:
Hello, I have some Macbinary files on a PC. I want to recursively read these files and remove the first 128 bytes of the files if they contain the macbinary header info. I know how to read...
8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
8
by: James Owens | last post by:
I'm a relative newbie, interested in storing the information from several server directories and subdirectories in XML so that I can present it selectively using XSL (all files updated today or...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
23
by: **Developer** | last post by:
Is there an easy way to copies all files in a directory into another directory? What about coping subdirectories too? Thanks in advance for any info
5
by: rbt | last post by:
What is the most efficient way to recursively remove files and directories? Currently, I'm using os.walk() to unlink any files present, then I call os.walk() again with the topdown=False option...
0
by: nhmark64 | last post by:
My PC was a completely screwed up mess. Nieither the unistall tool or the manual uninstall at http://msdn.microsoft.com/vstudio/support/uninstall/default.aspx worked. I also tried the VS...
9
by: David Pratt | last post by:
Hi. I'm trying to clean files for packaging on mac using os.path.walk and want to clean the .DS_Store files that are hidden from view but could end up in code that I produce. # Clean mac...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.