我希望编辑位于C:\CONFIG\TEST_CONFIG.XML
的XML文件,并替换其中的2个不同的值。我希望用这个文件替换以下标准:
<test.tbl>
CONFIG!CONFIG2 |1 T |125 **TN###** |151 **TN###**
</TEST.tbl>
"|125 TN###" must equal %USERINPUT1%
"|151 TN###" must equal %USERINPUT2%
我发现其他帖子已接近但难以绕过。
另一个问题是我需要保留所有间距和特殊字符。
答案 0 :(得分: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
编辑:已添加示例
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>
请务必在替换中使用正确匹配的字符串。在你的例子中,你把:
CONFIG!CONFIG2 |1 T |125 **TN###** |151 **TN###**
在您的数据中,但是:
"|125 TN###" must equal %USERINPUT1%
"|151 TN###" must equal %USERINPUT2%
在错过了**
数据中的匹配字符串中。我在替换中添加了**
,否则它们会失败......