I am looking to edit an XML file located in C:\CONFIG\TEST_CONFIG.XML and replace 2 different values within. I am looking to replace the following criteria with this file:

<test.tbl>
CONFIG!CONFIG2 |1 T |125 **TN###** |151 **TN###**
</TEST.tbl>

"|125 TN###" must equal %USERINPUT1%

"|151 TN###" must equal %USERINPUT2%

I have found other posts that come close but having difficulties getting around.

Another catch is I need to keep all the spacing and special characters.

有帮助吗?

解决方案

@echo off
setlocal DisableDelayedExpansion

set USERINPUT1=First Value
set USERINPUT2=Second Field

for /F "delims=:" %%a in ('findstr /N /C:"CONFIG!CONFIG2 |1 T |125 **TN###** |151 **TN###**" C:\CONFIG\TEST_CONFIG.XML') do set num=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" C:\CONFIG\TEST_CONFIG.XML') do (
   set "line=%%b"
   setlocal EnableDelayedExpansion
   if "%%a" equ "%num%" (
      set "line=!line:|125 **TN###**=%USERINPUT1%!"
      set "line=!line:|151 **TN###**=%USERINPUT2%!"
   )
   echo(!line!
   endlocal
)) > temp.xml
move /Y temp.xml C:\CONFIG\TEST_CONFIG.XML

EDIT: Example added

input.xml:

<test.tbl>
<p>Support the free distribution of this forecast by visiting our sponsors website.<p><b>Select forecast - </b>
<a href="?fdate=140403">Tomorrow</a> / <a href="?fdate=140404">Friday</a> / <a href="?fdate=140405">Saturday</a><p><hr>
CONFIG!CONFIG2 |1 T |125 **TN###** |151 **TN###**
<h5>Viewing forecast for Thursday, 3rd April, 2014</h5><p>Forecast last reviewed on Wednesday, 02/04/14 at 16:17
</TEST.tbl>

output.xml:

<test.tbl>
<p>Support the free distribution of this forecast by visiting our sponsors website.<p><b>Select forecast - </b>
<a href="?fdate=140403">Tomorrow</a> / <a href="?fdate=140404">Friday</a> / <a href="?fdate=140405">Saturday</a><p><hr>
CONFIG!CONFIG2 |1 T First Value Second Field
<h5>Viewing forecast for Thursday, 3rd April, 2014</h5><p>Forecast last reviewed on Wednesday, 02/04/14 at 16:17
</TEST.tbl>

Be sure to use the right matching strings in the replacements. In your example you put:

CONFIG!CONFIG2 |1 T |125 **TN###** |151 **TN###**

in your data, but:

"|125 TN###" must equal %USERINPUT1%
"|151 TN###" must equal %USERINPUT2%

in the matching strings that missed the ** in the data. I added the ** in the replacements, otherwise they fail...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top