473,385 Members | 2,003 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.

Reading the filename

Hello All,

I have a small problem in reading the filename of a file. I would like to have a script which could read my file and create the respective folders,

The file to be read is as follows d124_RD_SF_t01_r05_s089_z121_18grad.txt

folders and sub folders should be as follows

1. /x01_a05_s089/RD_z121_18grad

2. /x01_c05_s089/RD_z121_18grad

I have the following code

Expand|Select|Wrap|Line Numbers
  1. 1. #!/usr/bin/perl
  2. 2. use strict;
  3. 3. use File::Path;
  4. 4. use File::Copy;
  5. 5. print "Enter the file to copy: ";
  6. 6. $fl = <>;
  7. 7. chomp($fl);
  8. 8. my @dirs = qw[../../x01_a05_s089/RD_z121_18grad ../../x01_c05_s089/RD_z121_18grad];
  9. 9. for my $dir (@dirs) {
  10. 10. mkpath($dir) or die $!;
  11. 11. }
  12.  
But my question is the above code will work if u have the same input file. But my case is that input files varies. Only in the folder names x and s remain constant always and rest varies in my input filename.
For eg :if my next file is of the name d124_GD_SF_t02_r08_s091_z124_17grad.txt

my folders should be

1. /x02_a08_s091/GD_z124_17grad

2. /x02_c08_s091/GD_z124_17grad

Any suggestions would be helpful.
Sep 10 '08 #1
32 2364
numberwhun
3,509 Expert Mod 2GB
First, I think you are going to have to use a regular expression to pull the pieces out of the file name that you need and then populate the name of the directory from there.

On a side note.... I have corrected your code tags for your posting. You have 18 posts in the forums and have been had the code tags put into your posts before for you. PLEASE use code tags when posting code in the forums. If you do not know how to use them, then you can either edit your entry in this post and see them, or read this article that also shows you. Either way, please use them in the future.

Why not try working with the Regex's and if you need help, post here and we will help you.

Regards,

Jeff
Sep 10 '08 #2
eWish
971 Expert 512MB
Will the name be a fixed length? If so that would help you could use substr.

--Kevin
Sep 10 '08 #3
Yes it is fixed length. Is it right to define the variables separately as follows,

1. $file ='d124_RD_SF_t01_r05_s089_z121_18grad.txt'
2. $t1 =substr($file,5,2) # which is RD
3. $t2 =substr($file,11,12) # which is t01_r05_s089
and so on.....

Then I write the code in the form of defined variables. How do I combine two variables together. For example I have a folder name RD_z121_18grad.
Sep 10 '08 #4
eWish
971 Expert 512MB
You can concatenate two strings like so.

Expand|Select|Wrap|Line Numbers
  1. my $string1 = 'what';
  2. my $string2 = 'ever';
  3.  
  4. print $string1 . $string2;
perlop - Additive Operators

--Kevin
Sep 11 '08 #5
Thanks for the hint. I have another query.
Now that I have defined the filename in variable form like t1, t2 ,t3.
When i need to create new folders, I need to specify the directory path in terms of variables.
Code: ( text )

1. my @dirs = qw[../../x01_a05_s089/RD_z121_18grad];

How do I define the above path in terms of variables.
Sep 11 '08 #6
Adding to my earlier query, I am unable to use the replace function in substr.

From the filename, I store the particular file name as a variable and I would like to replace the firts variable. I tired this, but i find that it prints only the replaced letter. i would like to replace t01_r05_s089 as x01_a05_s089.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use strict;
  3. use File::Path;
  4. use File::Copy;
  5. print "Enter the file: ";
  6. my $file = <>;
  7. chomp($file);
  8. my $f1 = $file;
  9. my $t1=substr($f1,8,2);
  10. my $t2=substr($f1,23,12);
  11. my $t3 =$t1.$t2;
  12. my $t4=substr($f1,11,12);
  13. my $t5=substr($f1,11,12);
  14. print "$t5 \n";
  15. my $t6= substr $t5,0,12,'x';
  16. print "$t5 \n";
  17. print "$t6 \n";
  18.  
Also any suggestion for my previous query regarding the directory path interms of variables.
Sep 11 '08 #7
I have found the error in my code
my $t6= substr($t5, 0,1) = "x";
Sep 11 '08 #8
numberwhun
3,509 Expert Mod 2GB
Ramesh,

I pointed out earlier about your not wanting to use code tags and even provided a link that shows you HOW to use them. Putting numbers at the beginning of each line does not constitute for code tags.

This is the last time that I ask you to use the proper code tags. Please use them!

Regards,

Moderator
Sep 11 '08 #9
Hello Jeff,

Sorry about the code tags. I went through your article and I have listed only the code which I have tried. What is the error in my code tags.

Ramesh
Sep 11 '08 #10
numberwhun
3,509 Expert Mod 2GB
Hello Jeff,

Sorry about the code tags. I went through your article and I have listed only the code which I have tried. What is the error in my code tags.

Ramesh
The error was that you didn't use them. Code tags are not a starting "Code:" and they are just putting numbers at the beginning of each line of your code. The code tags take care of that. Look at the link that I posted earlier in this thread. It shows you what code tags look like. They start and end your block of code. All you have to do is edit your post where you put code and see how I did it.

Regards,

Jeff
Sep 11 '08 #11
NeoPa
32,556 Expert Mod 16PB
Hello Jeff,

Sorry about the code tags. I went through your article and I have listed only the code which I have tried. What is the error in my code tags.

Ramesh
Ramesh,

The "error" is that all code posted on these forums (with the possible exception of code that fits within a single displayable line) MUST be included in the [ CODE ] [ /CODE ] tags. This is made easy for you as there is a button (looks like a #) in the reply window that does it for you.

The link that Jeff posted in post #2 does explain all this for you in detail.

Repeatedly ignoring a moderator's instruction to add the tags when you post may well get you into trouble.

However, if this is simply a matter of not understanding the instructions we can be less strict with you. You must, of course, learn now that in ALL future posts you will be expected to add the tags where necessary. Follow the link. There is no more excuse for misunderstanding.

Please PM me if this is not clear. Otherwise we will all believe that you understand now.

I hope that answers your question.

-Administrator.
Sep 11 '08 #12
Hello Jeff now it is clear with your instructions. I would like to know your suggestion for my earlier query. I hope this query satisfies the code tags..

Now that I have split my filename in variable form like t1, t2 ,t3.
When i need to create new folders, I need to specify the directory path in terms of variables.
Expand|Select|Wrap|Line Numbers
  1. 1. my @dirs = qw[../../x01_a05_s089/RD_z121_18grad];
How do I define the above path in terms of variables.
Sep 11 '08 #13
numberwhun
3,509 Expert Mod 2GB
Hello Jeff now it is clear with your instructions. I would like to know your suggestion for my earlier query. I hope this query satisfies the code tags..

Now that I have split my filename in variable form like t1, t2 ,t3.
When i need to create new folders, I need to specify the directory path in terms of variables.
Expand|Select|Wrap|Line Numbers
  1. 1. my @dirs = qw[../../x01_a05_s089/RD_z121_18grad];
How do I define the above path in terms of variables.
First, yes, the code tags are much better, thank you!

I am assuming that the variables you created from your file name contain the information that is needed to make the path name. You would simply plug them in where they need to go. here is an example:

Expand|Select|Wrap|Line Numbers
  1. my $dirpath = qw[../../x${t1}_a${t2}_s${t3}/RD_z${t4}_${t5}grad];
  2.  
Know that this is only an example and you should plug your variables in where they go. The { and } that surround each variable name are simply separating it from the other text around it. Its a good idea to do this in such close quarters.

Also, for your code tags, no need to double number, the code tags already add numbering for you.

Regards,

Jeff
Sep 11 '08 #14
I have t3 defined as x02_a08_s091 and t5 defined as GD_z124_17grad. So when I need to create the folder, I get the folder with the name ${t5}/${t3} and not actual name.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use strict;
  3. use File::Path;
  4. use File::Copy;
  5. print "Enter the basis file: ";
  6. my $file = <>;
  7. chomp($file);
  8. my $f1 = $file;
  9. print "$f1 \n";
  10. my $t1=substr($f1,8,2);
  11. print "$t1 \n";
  12. my $t2=substr($f1,23,12);
  13. print "$t2 \n";
  14. my $t3 =$t1.$t2;
  15. print "$t3 \n";
  16. my $t4=substr($f1,11,12);
  17. print "$t4 \n";
  18. my $t5=substr($f1,11,12);
  19. my $t6= substr($t5, 0,1) = "x";
  20. my $t7= substr($t5, 4,1) = "b";
  21. print "$t5 \n";
  22. my $dirpath = qw[../../${t5}/${t3}];
  23. for my $dir (@dirs) {
  24. mkpath($dir) or die $!;
  25. }
Sep 11 '08 #15
In my earlier query I had typed my definition of my variable wrong, it is t3 as GD_z124_17grad. and t5 defined as x02_a08_s091. In the code it is defined properly.Still my problem is not solved as I get the folder defined in the form ${t5}/${t3} and not actual name.
Sep 12 '08 #16
KevinADC
4,059 Expert 2GB
Your code should do nothing because @dirs has never been defined anywhere in the code you posted:

Expand|Select|Wrap|Line Numbers
  1. for my $dir (@dirs) { <----- @dirs appears to be empty when used here
  2. mkpath($dir) or die $!;
  3. }
  4.  
Sep 12 '08 #17
Sorry I had typed the code wrong

Expand|Select|Wrap|Line Numbers
  1. my @dirs = qw[../../${t5}/${t3}];
  2. for my $dir (@dirs) {
  3. mkpath($dir) or die $!;
  4. }
But even then I am unable to get the folders with the right name.

To make a simple test I tried rewriting the code as follows

Expand|Select|Wrap|Line Numbers
  1. mkdir ../../$t5/$t3;
- It says folder not found.
Sep 12 '08 #18
The test with mkdir function works well
Expand|Select|Wrap|Line Numbers
  1. mkdir("../../x${s1}_a${s2}_s${s3}") || print $!;
  2. mkdir("../../x${s1}_a${s2}_s${s3}/GD_z${s4}_${s5}grad") || print $!;
I am able to get the actual name in my new folder as /x02_a08_s091/GD_z124_17grad. I guess there is some problem in my loop as it doesnot give the desired folder name.

Expand|Select|Wrap|Line Numbers
  1. my @dirs = qw[../../x${s1}_a${s2}_s${s3}/GD_z${s4}_${s5}grad];
  2. for my $dir (@dirs) {
  3. mkpath($dir) or die $!;
  4. }
I get my folder name in variable form as x${s1}_a${s2}_s${s3}
Sep 12 '08 #19
NeoPa
32,556 Expert Mod 16PB
Sorry I had typed the code wrong
...
I hope this tip proves helpful to you (and many others) in future :
NEVER type code into a post from your program!

Copy / Paste is your friend.
It allows others to see EXACTLY what your situation is, rather than a version filtered through a human brain (Very clever things, human brains - but filtering is absolutely not required at all for this).
Sep 12 '08 #20
If you see my previous posts, I have been trying my own code. I am now trying to write my code which works for all files in variable form. Right from yesterday I have been waiting for your reply and it is surprising that I have not go the right solution till now. I am very sorry to hear that you have badly interpreted by saying that I have copied the code.
Sep 12 '08 #21
KevinADC
4,059 Expert 2GB
change this line:

Expand|Select|Wrap|Line Numbers
  1. my @dirs = qw[../../x${s1}_a${s2}_s${s3}/GD_z${s4}_${s5}grad];
change to:

Expand|Select|Wrap|Line Numbers
  1. my @dirs = ("../../x${s1}_a${s2}_s${s3}/GD_z${s4}_${s5}grad");
"qw" does not expand/interpolate variables.
Sep 12 '08 #22
KevinADC
4,059 Expert 2GB
If you see my previous posts, I have been trying my own code. I am now trying to write my code which works for all files in variable form. Right from yesterday I have been waiting for your reply and it is surprising that I have not go the right solution till now. I am very sorry to hear that you have badly interpreted by saying that I have copied the code.
Thats not what he is saying. He is saying you should copy and paste your code into your posts so you don't make mistakes:

Sorry I had typed the code wrong
Sep 12 '08 #23
Thanks for the help....The code works now.
I went further with my code and got stuck up with this problem. I open my file and edit the file with replace function save it again.

Expand|Select|Wrap|Line Numbers
  1. open (IN,'+<','../../x02_a08_s091/GD_z124_17grad/d124_GD_SF_t02_r08_s091_z124_17grad.txt'); 
  2. open OUT,"+< ../../x02_a08_s091/GD_z124_17grad/d124_GD_SF_t02_r08_s091_z124_17grad.txt"; 
  3. while (<IN>) {
  4. s/rat/bat/g;
  5. print OUT;
  6. }
  7. close IN;
  8. close OUT;
The above code works well. But when I write it in terms of defined variables, It doesnot work.

Expand|Select|Wrap|Line Numbers
  1. # open (IN,'+<','../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}zoll/$f1'); 
  2. # open OUT,"+< ../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}zoll/$f1";
where
Expand|Select|Wrap|Line Numbers
  1. $f1= d124_GD_SF_t02_r08_s091_z124_17grad.txt
  2. $f2= d124_GD_SF_k02_l08_s091_z124_17grad.txt
Similarly when i use rename function interms of variables, it is not renaming my file.
Expand|Select|Wrap|Line Numbers
  1. rename('../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}grad/$f1','../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}grad/$f2');
Is the representation different when variables are included?
Sep 13 '08 #24
NeoPa
32,556 Expert Mod 16PB
Thats not what he is saying. He is saying you should copy and paste your code into your posts so you don't make mistakes:
Thanks for that Kevin.

Ramesh, Kevin is absolutely right I was certainly NOT accusing you of copying other people's code.

I was suggesting you DO copy (as in Copy/Paste) your code into your post. That way you never get the type of problem you had earlier.

Sorry if I wasn't clear.
Sep 13 '08 #25
KevinADC
4,059 Expert 2GB
Thanks for the help....The code works now.
I went further with my code and got stuck up with this problem. I open my file and edit the file with replace function save it again.

Expand|Select|Wrap|Line Numbers
  1. open (IN,'+<','../../x02_a08_s091/GD_z124_17grad/d124_GD_SF_t02_r08_s091_z124_17grad.txt'); 
  2. open OUT,"+< ../../x02_a08_s091/GD_z124_17grad/d124_GD_SF_t02_r08_s091_z124_17grad.txt"; 
  3. while (<IN>) {
  4. s/rat/bat/g;
  5. print OUT;
  6. }
  7. close IN;
  8. close OUT;
The above code works well. But when I write it in terms of defined variables, It doesnot work.

Expand|Select|Wrap|Line Numbers
  1. # open (IN,'+<','../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}zoll/$f1'); 
  2. # open OUT,"+< ../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}zoll/$f1";
where
Expand|Select|Wrap|Line Numbers
  1. $f1= d124_GD_SF_t02_r08_s091_z124_17grad.txt
  2. $f2= d124_GD_SF_k02_l08_s091_z124_17grad.txt
Similarly when i use rename function interms of variables, it is not renaming my file.
Expand|Select|Wrap|Line Numbers
  1. rename('../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}grad/$f1','../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}grad/$f2');
Is the representation different when variables are included?
Try this:

Expand|Select|Wrap|Line Numbers
  1. open (IN,'+<','../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}zoll/$f1') or die "<../../x${s1}_b${s2}_e${s3}/GD_m${s4}_${s5}zoll/$f1> :$!";
See what happens.
Sep 13 '08 #26
Hi Admin,

I am sorry for the comment. I had misunderstood your comment as it was not clear.Now it is clear.

Ramesh
Sep 14 '08 #27
Hi Kevin

It is not recognizing the directory . .

<../../x08_b03_e056/FD_m276_17zoll/r172_GD_EF_v08_a03_e056_m276_17zoll.txt> :No
such file or directory at t2.pl line 53, <> line 1.

The folder name and file in the folder is the same as mentioned in the error but maybe the promt sysmbol is not recognized.
Sep 14 '08 #28
KevinADC
4,059 Expert 2GB
replace ../../ with the full path to the file and see if that helps.
Sep 14 '08 #29
I tried giving the full path, there is no change in the text file... Also I tired even the rename function, it is not renaming my file when code in variables.
Sep 14 '08 #30
KevinADC
4,059 Expert 2GB
OK, well, that is all the help I can give you.
Sep 14 '08 #31
Hi Kevin,

At last was able to make the code work well.

I had split the file name in this form

d124_${t1}_SF_t${s1}_r${s2}_s${s3}_m${s4}_${s5}gra d.txt instead of defining as $f1 directly. Also in the rename function, the code with variables works only with double quotes(") not doesn't work with single quotes. Same is the case with open function. It is really surprising!

Thanks for all the help provided and I really appreciate this forum for the quick responses.

Ramesh
Sep 14 '08 #32
KevinADC
4,059 Expert 2GB
Hi Kevin,

At last was able to make the code work well.

I had split the file name in this form

d124_${t1}_SF_t${s1}_r${s2}_s${s3}_m${s4}_${s5}gra d.txt instead of defining as $f1 directly. Also in the rename function, the code with variables works only with double quotes(") not doesn't work with single quotes. Same is the case with open function. It is really surprising!

Thanks for all the help provided and I really appreciate this forum for the quick responses.

Ramesh

My apology. I did not notice the single-quotes. You must use double-quotes when there are scalars in the string. Perl will not interpolate scalars inside of single-quotes. This is a very basic concept of perl and is generally one of the first things you learn.
Sep 14 '08 #33

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

Similar topics

14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
4
by: sashan | last post by:
Is the way to use DOM for an xml file as follows: 1) Read the file into a string 2) Call xml.dom.minidom.parseString(string)
18
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
2
by: JS | last post by:
Hello all! I have come on to a problem for which I am not able to find a solution searching the web. What I am trying to do is reading a log-file with a size of 1.3 GB. When reading it using...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
12
by: Felix85 | last post by:
here is my method for reading in a file: static room room::file2Room(int rnum){ ostringstream filename; filename << "../gamefiles/rooms/" << rnum << ".room"; ifstream...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
4
by: Miner Jeff | last post by:
Hello, I have a basic question about reading files. I have several data files where the filenames are identical except for a short (3 character) prefix. I inherited this code and the person who...
0
Guido Geurs
by: Guido Geurs | last post by:
I'm writing a program that list the contents of a CDrom and also the contents of the ZIP files. When there is a bad Zip file on the CD, the program keeps traying to reed the file and after +- 50...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.