PDA

View Full Version : Replace in file: How to replace a single string by multiple lines?


Dimitri
09-01-2004, 04:34 AM
[Thanks for this excellent tool, it saves many many hours and above all, is fun to use!]

Is there a way I can replace a single identifier in a file by multiple lines? It works almost correct when "Treat all characters as literals" is off (the default), but then it performs regular expressions which for example results in "(" and ")" not being copied into the dest file.
And when I turn "Treat all characters as literals" on I get the step error:
"Number of search and replacement strings must match."
(which goes away when I only replace with a single line, instead of multiple lines).

How can I replace a single string by multiple lines and not perform any reg-exing at the same time?

P.S.:
As "replace with" I use a file's content (using vbld_FSO.OpenTextFile's ReadAll), this file is edited by doc maintainers so I would _preferably_ like to keep it clean from any escape-sequences (if that would solve the "(" disappearing in some way).

[Edit: Using VBPro5.5 with WinXP Home SP1]

kinook
09-01-2004, 06:53 AM
Using the Replace in File action, you would need to use regular expressions to find or replace strings containing newlines (and escape out any special regex characters). For this special case, you could roll your own using the FileSystemObject (see attached).

Dimitri
09-01-2004, 09:42 AM
Thanks a lot, it's working fine!
Is there a problem with interpreting a single-line Find field (while "no regex" is enabled) as the user wanting to replace that with what's literally in the Replace field? It seems intuitive to me and took me some time to figure out what VBPro was telling me (that the number of lines has to be equal).

kinook
09-01-2004, 10:18 AM
By design, the current implementation allows replacing multiple literal values in a single Replace in File action, which precludes multi-line matches or replacement values. From the help:

For literal text, this can be a single search and replace value, with the 'no regular expressions' field checked. To search and replace multiple literal values, enter each value on a separate line (the number of search and replace lines must match). For instance, to change all occurrences of 'fox' with 'cat' and 'dog' with 'beagle', the find expression

fox
dog

and replace expression

cat
beagle

could be used.

Dimitri
09-01-2004, 11:16 AM
Yes, I understand (and had seen the multiline usage in one of the samples). However what I mean is, instead of treating a single source line (and 1 only!) with multiple dest lines as an error, couldn't it be interpreted as meaning 'replace this one thing with whatever is in dest'? Or provide this functionality in some other way (since it would be somewhat confusing behaviour otherwise), perhaps on a different new tab-panel in Replace in File properties?

Replace in File is very powerful, but I think it would be helpful if it could also do very basic (?) stuff such as this (without having to do regex 'trickery', or a script (thanks again :) ), the first need I at least had for Replace in File was replacing a single identifier with a paragraph in a readme.txt file (multiple times).

kinook
09-01-2004, 11:31 AM
Yes, I suppose it could handle this special case to support replacing a single line literal with multiple lines. We'll add it to the list.

Dimitri
09-02-2004, 05:52 AM
Thanks for considering it. I now think such behaviour would even make sense (as opposed to being a hidden feature) : Replace in file can already replace a single identifier by multiple lines when "Treat all characters as literals" is off. The only difference here is the checkbox is checked.