使用批处理文件中的findstr替换特定单词后的任何字符串

时间:2016-09-05 02:58:39

标签: batch-file

我编辑了这个问题。我现在正在做的是在特定的单词或字符后用字符串替换字符串。我想要的是用<xml></xml>替换内容为SampleValue-(whatever char after the dash)的所有xml标记SampleValue-Test。以下是我的代码:

@ECHO OFF
set "file=testinput.xml"
set "getValue=<xml>SampleValue-</xml>"
set "setValue=<xml>SampleValue-Test</xml>"
(
for /f "delims=" %%a IN ('findstr /N "^" %file%') DO (
set "line=%%a"
setlocal ENABLEDELAYEDEXPANSION
set "line=!line:%getValue%=%setValue%!"
echo !line:*:=!
endlocal
)
)>tmp
move /Y tmp output.xml

1 个答案:

答案 0 :(得分:0)

使用PowerShell可能是最简单的方法:

@ECHO OFF
set "inFile=testinput.xml"
set "outFile=testoutput.xml"
set "getValue=<xml>SampleValue-</xml>"
set "setValue=<xml>SampleValue-Test</xml>"

powershell -Command "(Get-Content %inFile%) -replace '%getValue%', '%setValue%' | Set-Content %outFile%"