# Edit XML file using a BAT



## spm5

Hello all, 
I am hoping someone can help me with a little problem I am having. 
I need to replace 1 part of 1 line in an XML file but it needs to be done on 500+ machines. The files that are being changed contain computer specific information so I cannot just overwite the entire file with a new one. The file contense is pasted below. Towards the bottom there is a line:
<ManagerName>localhost</ManagerName>
I need to change this to 
<ManagerName>10.100.1.57</ManagerName>
Apart from that I want the file to be the same (as you see just after the line i want to change there are lines for the indevidual PC's name and MAC)
I think this can be done with some fancy FOR command work but after hours of tinkering I just cant seem to get it. 
Can anyone help with this?



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE SystemConfiguration [
<!ELEMENT SystemConfiguration (GeneralCfg*, TimerCfg*, LogCfg*, AppDetails*)>
<!ELEMENT GeneralCfg (ManagerPort, MaxBlockSize, NetworkMask)>
<!ELEMENT ManagerPort (#PCDATA)>
<!ELEMENT MaxBlockSize (#PCDATA)>
<!ELEMENT NetworkMask (#PCDATA)>
<!ELEMENT TimerCfg (AckTimeout, AckRetries, FastTimeout)>
<!ELEMENT AckTimeout (#PCDATA)>
<!ELEMENT AckRetries (#PCDATA)>
<!ELEMENT FastTimeout (#PCDATA)>
<!ELEMENT LogCfg (LogError, LogWarning, LogInfo, LogDebug, LogDebugex)>
<!ELEMENT LogError (#PCDATA)>
<!ELEMENT LogWarning (#PCDATA)>
<!ELEMENT LogInfo (#PCDATA)>
<!ELEMENT LogDebug (#PCDATA)>
<!ELEMENT LogDebugex (#PCDATA)>
<!ELEMENT AppDetails ANY>
]>
<SystemConfiguration>
<GeneralCfg>
<ManagerPort>3234</ManagerPort>
<ServerPort>3233</ServerPort>
<MaxBlockSize>128000</MaxBlockSize>
<NetworkMask>255.255.255.0</NetworkMask>
<HttpRedirection>http://www.autodesk.com</HttpRedirection>
</GeneralCfg>
<TimerCfg>
<AckTimeout>20000</AckTimeout>
<AckRetries>6</AckRetries>
<FastTimeout>4000</FastTimeout>
</TimerCfg>
<LogCfg>
<LogError>1</LogError>
<LogWarning>1</LogWarning>
<LogInfo>1</LogInfo>
<LogDebug>0</LogDebug>
<LogDebugex>0</LogDebugex>
<LogMaxFileSize>10485760</LogMaxFileSize>
</LogCfg>
<AppDetails>
<ServerSettings>
<ManagerName>localhost</ManagerName>
<AutoSearch>0</AutoSearch>
<ServerName>kpfny-006</ServerName>
<ServerMAC>001CF0EEB3210000</ServerMAC>
<Description></Description>
</ServerSettings>
</AppDetails>
</SystemConfiguration>





Thanks


----------



## TheOutcaste

Welcome to TSF!

Because of all the batch redirection symbols (< and >) and parentheses, be much easier to do this with a VBS Script.



Code:


Const ForReading = 1
Const ForWriting = 2
strFile = Wscript.Arguments.Item(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(StrFile, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "localhost", "10.100.1.57",1,-1,1)
Set objFile = objFSO.OpenTextFile(StrFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Save this as EditXMLFile.vbs
Run it with this command:
cscript //nologo editxmlfile.vbs "filename.xml"

This will work remotely if you have the correct permissions, so you can do eather of these:
cscript //nologo editxmlfile.vbs "\\server\Share\<path>\filename.xml"
cscript //nologo editxmlfile.vbs "\\server\c$\<path>\filename.xml"

If the path is the same on each PC and you have a list of PCnames in a text file. a batch files and a simple For loop can be used to update each PC remotely, and keep a log of those that have been changed and those that haven't.


----------

