3

I have a template file (say myTemplate.txt) and I need to make some edits to create my own file (say myFile.txt) from this template.

So the template contains lines like

env.name=
env.prop= 
product.images.dir=/opt/web-content/product-images

Now I want this to be replaced as follows;

env.name=abc
env.prop=xyz
product.images.dir=D:/opt/web-content/product-images

So I am looking for batch commands to do the following;

1. Open the template file.
2. Do a kind of find/replace for the string/text
3. Save the updates as a new file

How do I achieve this ?

1 Answer 1

6

The easiest route is to modify your template to look something like this:

env.name=!env.name!
env.prop=!env.prop!
product.images.dir=/opt/web-content/product-images

And then use a FOR loop to read and write the file while delayed expansion is enabled:

@echo off
setlocal enableDelayedExpansion
set "env.name=abc"
set "env.prop=xyz"
(
  for /f "usebackq delims=" %%A in ("template.txt") do echo %%A
) >"myFile.txt"

Note it is much faster to use one over-write redirection > for the entire loop then it is to use append redirection >> within the loop.

The above assumes that no lines in template begin with ;. If they do, then you need to change the FOR EOL option to a character that will never start a line. Perhaps equal - for /f "usebackq eol== delims="

Also the above assumes the template doesn't contain any blank lines that you need preserved. If there are, then you can modify the above as follows (this also eliminates any potential EOL issue)

@echo off
setlocal enableDelayedExpansion
set "env.name=abc"
set "env.prop=xyz"
(
  for /f "delims=" %%A in ('findstr /n "^" "template.txt"') do (
    set "ln=%%A"
    echo(!ln:*:=!
  )
) >"myFile.txt"

There is one last potential complicating isse - you could have problems if the template contains ! and ^ literals. You could either escape the chars in the template, or you could use some additional substitution.

template.txt

Exclamation must be escaped^!
Caret ^^ must be escaped if line also contains exclamation^^^!
Caret ^ should not be escaped if line does not contain exclamation point.
Caret !C! and exclamation !X! could also be preserved using additional substitution.

extract from templateProcessor.bat

setlocal enableDelayedExpansion
...
set "X=^!"
set "C=^"
...
Sign up to request clarification or add additional context in comments.

2 Comments

This is great! thank you. I had to make a minor tweak to make it work from a callable bat, which was to change "set ln=%%A" to set "ln=%%A" (note the placement of the first dbl-quotation). In addition, to use a var called template_file, I replaced template.txtwith ^"^"^"%%template_file%%^"^"^"
@mwag - Thanks. I'm surprised nobody caught the typos before you - the answer is 8 years old! I edited my answer and moved the quote to where it belongs. I also changed the mistaken backtick in the FOR /F command to the correct single quote. Your parameterization of the template file is over complicated - there is no need for delayed expansion or escaped quotes. You could simply use "%template_file%". Or if you want to specify the template as the first batch argument, then use "%~1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.