1.

Solve : change key/value pairs in xml file from dos batch?

Answer»

Hi All,

One part of my dos bat file needs to parse an xml file LOOKING for a "key" then get the value from next line down, then ask user to change it, then edit/replace OLD value with new value.

Been at this for days now, looking for guidance, TIA.

Here is my lines in xml file:

111


need to prompt user showing number "111" then ask to replace 111.

I have tried MANY combos of find/findstr/for/if loops but no no avail.
Looking for fresh input and knowledge.

TIA, frustrated cpatte!Quote from: cpatte on October 29, 2009, 07:39:37 AM



Here is my lines in xml file:
<param name="license_ports">
<value>111</value>
</param>


We used commandline argument but can easily change to prompt:


C:\>type patte.bat
Code: [Select]@echo off

findstr /v "111" patte.txt > newpatte.txt

findstr /v "</param>" newpatte.txt > 2newpatte.txt

echo "<value>%1</value>" >> 2newpatte.txt

echo "</param>" >> 2newpatte.txt
OUTPUT:

C:\> patte.bat 113

C:\>type 2newpatte.txt
Here is my lines in xml file:

"113"
""

C:\>

I leave it to Cpatte to remove " (quotes) and add a prompt for user.

If Cpatte needs further help I will do the above.

Good luck

p.s. The Ghost will now tell US the Unix sed ( Stream Editor ) command can edit your file with one line of code. Sed is available for windows and will work inside a batch file.Quote from: cpatte on October 29, 2009, 07:39:37 AM


<param name="license_ports">
<value>111</value>
</param>



C:\> sed 's/111/113/' pattie.txt

OUTPUT:



113



C:\>Quote from: cpatte on October 29, 2009, 07:39:37 AM

<param name="license_ports">
<value>111</value>
</param>



C:\>type patsed.bat
Code: [Select]REM Use commandline argument: patsed.bat 113
@echo off
sed 's/111/%1/' pattie.txt
OUTPUT:

C:\>patsed.bat 113

C:\>REM Use commandline argument: patsed.bat 113



113



C:\>here's a vbscript.
Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
strInput = ""
strOutput = "c:\test\output.txt"
Set objFile = objFS.OpenTextFile(strFile,1)
Set objOutFile = objFS.CreateTextFile(strOutput,TRUE)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"license_ports")> 0 Then
WScript.Echo "Enter new value: "
Do While Not WScript.StdIn.AtEndOfLine
strInput = strInput & WScript.StdIn.Read(1)
Loop
f=1
End If
If f=1 And InStr(strLine,"value") > 0 Then
strLine = "<value>"&strInput&"</value>"
f=0
End If
objOutFile.WriteLine(strLine)
Loop
objFile.Close
objOutFile.Close

output
Code: [Select]C:\test>more file
<param name="license_ports">
<value>111</value>
</param>
<param name="other">
<value>1888</value>
</param>


C:\test>cscript /nologo test.vbs
Enter new value:
876

C:\test>more output.txt
<param name="license_ports">
<value>876</value>
</param>
<param name="other">
<value>1888</value>
</param>

Quote from: billrich on October 29, 2009, 12:46:28 PM

Code: [Select]@echo off

findstr /v "111" patte.txt > newpatte.txt
findstr /v "</param>" newpatte.txt > 2newpatte.txt
echo "<value>%1</value>" >> 2newpatte.txt
echo "</param>" >> 2newpatte.txt
your method will not work when there are mulitiple tags, unless OP's XML file is just those 3 lines. Same to your sed solution.


Quote
p.s. The Ghost will now tell us the Unix sed ( Stream Editor ) command can edit your file with one line of code. Sed is available for windows and will work inside a batch file.
don't put words in my mouth. I would not even consider sed AT ALL. The ideal solution this is to use a proper XML parser. If not, use a language that can process multiline strings easily, have regex capabilities (although its not a must), and let me/others easily read and understand my code.


Discussion

No Comment Found