0

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.

2
  • Does that config line appear anywhere else in the file exactly the same way? Commented Apr 2, 2014 at 17:19
  • No, this does not appear anywhere else in the file. Commented Apr 2, 2014 at 17:39

1 Answer 1

1
@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...

Sign up to request clarification or add additional context in comments.

6 Comments

I am getting "The system cannot find the file 'findstr /N /C:"CONFIG!CONFIG2 |1 T |125 TN### |151 TN###" C:\CONFIG\TEST_CONFIG.XML.
Oops! There was a small bug... Fixed! ;-)
This really messes with my XML file resulting in the following: C:\CONFIG>( set "line=" setlocal EnableDelayedExpansion if "2279" EQU "576" ( set "line=!line:|125 **TN###**=123!" set "line=!line:|151 **TN###**=456!" ) echo(!line! endlocal ) This goes on and repeats itself for over 23,000 lines
Good catch, I was more using the "" to highlight/bold the fields I needed changed. Although I did remove the "" from the batch entirely now and still getting: C:\CONFIG>( set "line=" setlocal EnableDelayedExpansion if "2279" EQU "576" ( set "line=!line:|125 TN###=123!" set "line=!line:|151 TN###=456!" ) echo(!line! endlocal ) with over 27,000 lines
I suggest you to copy my example data above, run the program and confirm that you get my example output. If so, then you should analyze the differences of your real data vs. the example you posted that was the base for my program. Hint: execute findstr /N /C:"CONFIG!CONFIG2 |1 T |125 TN### |151 TN###" C:\CONFIG\TEST_CONFIG.XML; you should get one result line...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.