Changing tag data in an XML file using windows batch file

You really should use a tool that is designed specifically to manipulate XML.

But in a pinch, you could use any tool that can do a regex search and replace to do a naive solution that would work with the file as you have it laid out, but might fail with a logically equivalent XML file that has had the physical layout rearranged.

I like to use a hybrid JScript/batch utility I wrote called REPL.BAT to manipulate text files via batch scripts. The script will run on any native Windows machine from XP onward, and it does not require installation of any 3rd party executables. Click the link to get the script code and a more thorough description.

Using REPL.BAT, a fast and efficient but naive solution is as simple as:

setlocal enableDelayedExpansion
set "newValue=myNewValue"
type "fileName.xml"|repl "(<InstallationType>).*(</InstallationType>)" "$1!newValue!$2" >fileName.xml.new
move /y "fileName.xml.new" "fileName.xml"

Leave a Comment