Replace File Name with text within file

General discussions about Advanced Find and Replace
Post Reply
myother_acct@hotmail.com
Posts: 2
Joined: Fri Nov 10, 2006 6:29 am

Replace File Name with text within file

Post by myother_acct@hotmail.com » Fri Nov 10, 2006 6:37 am

I'd like to replace a files which have names like

001.rec

with names taken from within the file. For example, the content of the above file is:

N082 2
8.0480 8.0480
1.0000 0.0000
0.0310 0.0630
ROLLS:
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
I'm a registered user of version 2.4, and I've used this program to solve many problems!

Abacre
Site Admin
Posts: 1201
Joined: Mon Jan 31, 2005 5:32 pm
Contact:

Post by Abacre » 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:
$3.rec\r\n$2$3$4

Secondly:
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:
$3.dat\r\n$2$3$4

Thirdly:
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.

Then:
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:

Search For:
(\d+)\.rec\r\n(.*)(N\d+)

Replace With:
$3.dat\r\n$2$3

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.
Kind regards,
Abacre Limited
http://www.abacre.com
support@abacre.com

myother_acct@hotmail.com
Posts: 2
Joined: Fri Nov 10, 2006 6:29 am

It worked -- I almost understand...

Post by myother_acct@hotmail.com » 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
$$READ/20,ALL,WSROLL
PARTNO1042 (P20557**.042) G:\PR20557\W042.DAT 8T2 42 11/09/06 : 15:38
INCLUD/ DKA300:[SOURCE.100001]MSROLL.MAC
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
I'm a registered user of version 2.4, and I've used this program to solve many problems!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests