Page 1 of 1
Replace File Name with text within file
Posted: Fri Nov 10, 2006 6:37 am
I'd like to replace a files which have names like
with names taken from within the file. For example, the content of the above file is:
817 4 5.9870 18T1 B 6 0.0000 1.0000
The new file name in this case would be N082.DAT, that is, the name is taken from the "N###" found within the file, along with a .DAT extention.
I tried (\d+)\.rec\r\n(.*)(N0\d+)(.*) and replacing with $3\.rec\r\n$2$3$4 but it didn't work.
Thank you for any help. I have dozens of these files
Posted: Fri Nov 10, 2006 10:59 am
There are few errors in your regular expressions.
First of all:
Your Replace With part cannot be compiled because of error in char #3.
In fact: you should escape reserved chars in Search For part
for example escape dot with \ like you correctly did: \.
But you don't need to escape it in Replace With part:
so you should simply use:
You said the resulting file name should have the .DAT extension.
But you mentioned .REC in your Replace With part.
Therefore the expression should be:
you use (.*) at the end of Search For part and you later use $4 in
Replace With part. This (.*) makes nothing. So don't use it.
You said N### where # means a digit. But you used: N0\d+ in Search
For. So you should use N\d+
Finally the resulting expression should be:
Remember that you should put this pair into Batch Replace grid. And
you should check "Use Regular Expressions" option.
Now you should carefully setup the needed options.
Go to main menu - Action - Options - Batch Replace
uncheck "Replace in File Names"
check "Replace in File Bodies"
check "Modifier S"
check "Modifier G"
check "Include File Name into Search Scope"
check "Rename File Name if it was changed"
That's all. I verified it works perfectly.
It worked -- I almost understand...
Posted: Fri Nov 10, 2006 8:33 pm
Thank you very much for your quick response to my question. As your comments suggest, I'm certainly no expert at regular expresions, and your help is appreciated.
In order to get it to work, however, I had to change a few things. First of all, I needed to upgrade to version 2.4 since the "Rename File Name if it was changed" checkbox does not appear in 2.3. Secondly, I needed to alter the \d+ part to \d\d\d for some reason. Fortunately, all the changes involved 3 digits so this worked. So here is what ended up working for me:
(\d\d\d)\.rec\r\n(.*)(N\d\d\d) in the find and
$3.dat\r\n$2$3 in the replace
I really want to understand this since it is so powerful. Here are 3 more questions:
1) I do not understand why the \r\n is needed?
2) Am I correct in assuming that when AFR searches a file it's as if the filename appears at the top of the file?
3) When I tried to do a similar search on the following file, it didn't work. I don't understand why since the only difference is the use of a W instead of an N. That is, I want the files named W###.dat this time. I used
(\d\d\d)\.rec\r\n(.*)(W\d\d\d) in the find and
$3.dat\r\n$2$3 in the replace
but it didn't work. A sample file is called 168.rec and it's contents are below
$$42 MAXEL 1 3 8.50 1.2500 1.0000 2.0010
PARTNO1042 (P20557**.042) G:\PR20557\W042.DAT 8T2 42 11/09/06 : 15:38
D1=DATA/ 7.2807, 8.0480, 8.0480
Z1=DATA/ 1.0000, 0.5000, 0.0000
RAD1=DATA/ 0.0630, 0.0600, 0.0630
CALL/MS1,MTYP=2,BS= 8.50,LC= 3,LMC= 3,FP=1
FINI $$ 1.2500 1 1.0000
W 0.0000 0.0000 0.0000
3 7.3470 8T2