5

I have an XML file with several tags in it, and I am trying to change the contents of one of these tags with a batch script.

An example of the xml file is:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>Terminal</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>

I am trying to change the data between <InstallationType> and </InstallationType> to another variable of my choosing. I'm thinking I would need some sort of For loop to find that part of the xml file, but I'm lost as to how I would edit just that one section.

user2382144
  • 207
  • 2
  • 4
  • 9
  • Duplicate of http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir please check the solutions there – Yuriy Galanter Jun 11 '13 at 21:41
  • http://www.dostips.com/DtTipsStringManipulation.php – Ken White Jun 11 '13 at 21:51
  • Not exactly. All of the answers in that question are more "brute force" find and replace for all instances of a specific string. In my question, I just want to change data between two XML tags, and I don't necessarily know what data will originally be there. – user2382144 Jun 11 '13 at 21:54
  • Do you have no access to tools which can correctly manipulate XML? Like a programming language, or even VBSCRIPT? – John Saunders Jun 12 '13 at 04:53

3 Answers3

13

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"
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 4
    @JohnSaunders - So I warn the OP that an XML aware tool should be used, and that a regex "solution" can fail with a valid, rearranged XML file input. Yet you still think the answer deserves a -1 for showing how to use regex to manipulate the OP's layout?, even though it is clearly stated that it will not work reliably if the input XML physical layout changes? That seems harsh and arbitrary, especially considering Aacini's answer is spared your admonishment even though it is even more XML naive than the regex "solution". At least you explained your reason in comment; thanks for that courtesy. – dbenham Jun 12 '13 at 11:00
  • 2
    @JohnSaunders: I reviewed your profile and I realized that you had posted just ONE answer on Batch-file tag topics from your 4,860 total answers, and that it was a negative answer against a Batch solution. I courteously encourage you to keep your answers and comments in your expertise area, that certainly is not Batch files... Thanks a lot. – Aacini Jun 12 '13 at 11:52
11
@echo off
setlocal EnableDelayedExpansion

set anotherVariable=New value

(for /F "delims=" %%a in (theFile.xml) do (
   set "line=%%a"
   set "newLine=!line:InstallationType>=!"
   if "!newLine!" neq "!line!" (
      set "newLine=<InstallationType>%anotherVariable%</InstallationType>"
   )
   echo !newLine!
)) > newFile.xml

Output with your example data:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>New value</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Hmm This is definitely getting close. It get an error indicating that the process cannot access the file because it is being used by another process, and the file gets blanked out. Yet, I have no issues editing the file any other way. – user2382144 Jun 11 '13 at 23:05
  • That error is not related to the Batch code in any way. Perhaps you have the input or output .xml files open in an editor or other program at the same time the Batch file run? – Aacini Jun 12 '13 at 01:39
  • That was the first thing I checked. I tried running it on a separate xml file as well that hadn't been previously opened, and I received the same error. – user2382144 Jun 12 '13 at 01:47
  • Execute this line before run the Batch file: `type theFile.xml > newFile.xml`; double-check the names you use in the Batch file vs. the real names of the files. If this doesn't work, close the MS-DOS command Window and open it again... – Aacini Jun 12 '13 at 02:00
1

GNU sed

sed "/InstallationType/s/>[^<]*</>New Value</" file.xml

..output:

<ConfigurationData>
<ReportsFolder>\Reports</ReportsFolder>
<Helpfolder>\Help</HelpFolder>
<InstallationType>New Value</InstallationType>
<LicenseFolder>..\License</LicenseFolder>
</ConfigurationData>
Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63