我在尝试让这个批处理文件正常工作时遇到了一些问题。我已经尝试了几种不同的方法来查找和替换字符串。
问题:我们必须定期更新数据库,这样做会更改一个文件。我必须将一个文件改回来以使事情正常工作。这行代码:
<network ipAdress="172.24.55.32" networkPort="9100" />
被更改为这行:
<file filename="fffff" />
需要将其更改回带有IP地址的第一行。
我尝试使用查找和替换子例程,并可以在更改“file filename”为“network ipAdress”的第一部分上使其正常工作,但第二个部分中的引号使脚本无法正常工作。以下是我拥有的代码:
@echo off
setlocal
call :FindReplace "file filename" "network ipAdress" BCPrint.XML
Timeout 2
call :FindReplace "fffff" "172.24.55.32" networkPort="9100" BCPrint.XML
exit /b
:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
<%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
)
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
我也尝试将第二部分设置为变量,例如:
set R2=172.24.55.32" networkPort="9100
call :FindReplace "file filename" "network ipAdress" BCPrint.XML
Timeout 2
call :FindReplace "fffff" "%R2%" BCPrint.XML
同时使用转义字符:
set ^R2=172.24.55.32" networkPort="9100^
我相信有一些我没有列出来的不同方法,比如不使用变量的转义字符。但我已经尽力想到的都试过了。
有没有一种方法可以用带有特殊字符的行替换另一行呢?如果能告诉我如何替换整行或第二个带引号和空格的部分,那就太好了。谢谢您的时间。
set "S2=fffff" & set "R2=172.24.55.32" networkPort="9100"
,2.call :FindReplace S2 R2 BCPrint.XML
,3. 分别用!%~1!
和!%~2!
替换%~1
和%~2
,4. 在:FindReplace
后面放置setlocal EnableDelayedExpansion
,在del
和exit /B
之间放置endlocal
... - aschipfl