Replacing contents of a text file with batch

I personally do not like always creating applications for basic tasks. This is even the case when writing an application is probably easier than doing the same with batches, but I like these challenges. In my case, I had a few videos I wanted to process with AviSynth. Rather than creating multiple copies of the AviSynth script, I wanted to create a template where all other derive from.

Reading a file line by line

The file will be processed line by line. We can achieve this with a simple for-loop. The following example will simply cycle through all lines of the file input.txt and print them to the console. The %%a variable will hold the text of the currently processed line.

A little drawback is that this method will strip empty lines and lines starting with a semicolon. The parameter usebackq is required in order to use quotes within the filename set (here “input.txt”).

You can avoid stripping of lines beginning with a semicolon by passing an alternative eol symbol. In the following example, I’ll use a colon as an alternative.

Replacing placeholders

As we’re now able to read a file line by line, next thing on the list to start replacing our placeholders. To avoid invalid matches, I used the special combination @@ at the beginning and the end of the variable (no special reason behind this).

Replacement with variables is no special task either. But due to the nesting of variables, we have to make use of call.

The reason for the use of the call command is required in order to nest variables. With the execution of call, the first set of variables will be interpreted. In this case, this would be VARIABLE_NAME and NEW_VALUE. After this, the actual set command is being executed where the string replacement is being run on the CURRENT_LINE variable.

The use of the double %-symbol is used to escape the %. So therefore, during the execution of call, the %% will change to %. The execution will somehow look like this.

Processing the template file

Finally we have to combine the file reading, the string replacement and writing the output file. We use the the >> operator instead of the > one. The difference between these two is, the former appends lines to a file whereas the latter owerwrites the file.

I split the replacement from the for loop because it would require delayed expansion. It would also be possible (and probably cleaner) to create a separate batch file for this.

Example Code

References

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.