-1

I am trying to create a which will install an agent in a silent manner. Once the agent has installed I will need to navigate into the agent directory and edit a file called agent.xml.

The agent.xml file looks like the below:

<?xml version="1.0" encoding="utf-16"?>
<Details xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Proxy />
<ProxyDomain />
<ProxyUsername />
<ProxyPassword />
<UseNewHub>true</UseNewHub>
<Thumbprint />
<Url>https://localhost/api/</Url>
<U>agent</U>
<P />
<E1>gr\atga\rgr\zergeesfsdgfsg245325252sgsgsdfgssdgs3535353</E1>
</Details>

I am trying to replace the

<Thumbprint /> 

line with this:

<Thumbprint>213GARFASF131231FAAAF2</Thumbprint>

Can anyone help me implement a command that will find and replace that thumbprint line?

Compo
  • 36,585
  • 5
  • 27
  • 39
Help
  • 161
  • 1
  • 2
  • 14
  • 2
    The top of each page has a search function, try starting with [\[batch-file\]\[xml\]replace line](https://stackoverflow.com/search?q=%5Bbatch-file%5D%5Bxml%5Dreplace+line). – Compo May 31 '19 at 09:07
  • 2
    Batch files are not particularly suited for text processing (e.g. https://stackoverflow.com/questions/39209968/search-and-replace-xml-attribute-using-file-name-as-the-attribute-value-powersh), it would be much easier if you could install gnuwin32 (http://gnuwin32.sourceforge.net/) or a higher level language (e.g. python)... – thebjorn May 31 '19 at 09:13
  • Thank you for the feedback, do you know if there would be a way of doing this using powershell? – Help May 31 '19 at 09:17

1 Answers1

2

First of all, this link has numerous methods of doing this flexibly.

If you want a quick method, assuming that what you want to replace will always be in the form of <Thumbprint />, and there's no ! in your file, you can use this:

setlocal enabledelayedexpansion
for /f "delims=" %%a in (input.xml) do (
    set line=%%a
    echo !line:Thumbprint /=Thumbprint^>213GARFASF131231FAAF2^</Thumbprint^!>>output.xml
)

Replacing input.xml and output.xml.

BDM
  • 3,760
  • 3
  • 19
  • 27
  • 1
    Do you have seen `encoding="utf-16"` in first line of provided content of `agent.xml`? The XML file to process is a UTF-16 encoded Unicode file. So even if __FOR__ processes UTF-16 encoded `agent.xml` with ignoring all empty lines, ignoring all lines starting with a semicolon and with removing all text between two exclamation marks as well as every `!` which all could be no problem according to provided XML content, the output XML file is declared as UTF-16 encoded although being ANSI encoded. – Mofi May 31 '19 at 09:46
  • @BDM thank you so much. I tried to replace the input.xml and output.xml file with the file name I am trying to modify (agent.xml) and this has modified the thumbprint parameter and has also duplicated all entries within the file, do you know where I went wrong? – Help May 31 '19 at 10:11
  • @Hasan: you can't read a file and write to it at the same time, that's why `input.xml` and `output.xml` were chosen. (well in fact - you can read a file and *append* to it at the same time - which is exactly what you described). write to a new file and afterwards, if needed, rename it to the original name. – Stephan May 31 '19 at 14:34