Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 在批处理文件中使用findstr替换特定单词后的任何字符串_Batch File - Fatal编程技术网

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

Batch file 在批处理文件中使用findstr替换特定单词后的任何字符串,batch-file,Batch File,我编辑了这个问题。我现在要做的是用特定单词或字符后面的字符串替换字符串。我想要的是将所有xml标记替换为内容SampleValue-(破折号后的任何字符)替换为SampleValue Test。下面是我的代码: @ECHO OFF set "file=testinput.xml" set "getValue=<xml>SampleValue-</xml>" set "setValue=<xml>SampleValue-Test</xml>" ( f

我编辑了这个问题。我现在要做的是用特定单词或字符后面的字符串替换字符串。我想要的是将所有xml标记
替换为内容
SampleValue-(破折号后的任何字符)
替换为
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
@ECHO关闭
设置“file=testinput.xml”
设置“getValue=SampleValue——”
设置“设置值=采样值测试”
(
对于/f“delims=“%%a IN('findstr/N“^”%file%')DO(
设置“行=%%a”
setlocal ENABLEDELAYEDEXPANSION
设置“行=!行:%getValue%=%setValue%!”
回音!行:*:=!
端部
)
)>tmp
move/Y tmp output.xml

使用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%"
@ECHO关闭
设置“infle=testinput.xml”
设置“outFile=testoutput.xml”
设置“getValue=SampleValue——”
设置“设置值=采样值测试”
powershell-命令“(获取内容%infle%)-替换“%getValue%”、“%setValue%”|设置内容%outFile%”

使用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%"
@ECHO关闭
设置“infle=testinput.xml”
设置“outFile=testoutput.xml”
设置“getValue=SampleValue——”
设置“设置值=采样值测试”
powershell-命令“(获取内容%infle%)-替换“%getValue%”、“%setValue%”|设置内容%outFile%”

您知道,您可以将变量=值对括在引号中,以消除转义的需要。示例:
设置“getValue=SampleValue-.*”
。然后,
for/f“tokens=3 delims=-我编辑了我的问题。谢谢:)问题是我应该怎么做,用我的
setValue
替换文本替换破折号后面的字符串。你知道,你可以用引号括住变量=值对,以消除转义的需要。例如:
set“getValue=SampleValue-*“
。然后,
for/f“tokens=3 delims=-我编辑了我的问题。谢谢:)问题是我应该怎么做,以我的
setValue
作为替换文本替换破折号后面的任何字符串