By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

Reading the filename

P: 37
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
Share this Question
Share on Google+
32 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Expert 100+
P: 971
Will the name be a fixed length? If so that would help you could use substr.

--Kevin
Sep 10 '08 #3

P: 37
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
Expert 100+
P: 971
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

P: 37
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

P: 37
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

P: 37
I have found the error in my code
my $t6= substr($t5, 0,1) = "x";
Sep 11 '08 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
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

P: 37
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
Expert Mod 2.5K+
P: 3,503
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
Expert Mod 15k+
P: 31,530
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

P: 37
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
Expert Mod 2.5K+
P: 3,503
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

P: 37
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

P: 37
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
Expert 2.5K+
P: 4,059
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

P: 37
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

P: 37
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
Expert Mod 15k+
P: 31,530
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

P: 37
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
Expert 2.5K+
P: 4,059
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
Expert 2.5K+
P: 4,059
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

P: 37
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
Expert Mod 15k+
P: 31,530
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
Expert 2.5K+
P: 4,059
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

P: 37
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

P: 37
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
Expert 2.5K+
P: 4,059
replace ../../ with the full path to the file and see if that helps.
Sep 14 '08 #29

P: 37
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
Expert 2.5K+
P: 4,059
OK, well, that is all the help I can give you.
Sep 14 '08 #31

P: 37
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
Expert 2.5K+
P: 4,059
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

Post your reply

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