问题
I have a .properties file say abc.properties file. It has a text BuildNumber=0. I want to search the BuildNumber and replacw its value with current build number through batch file.
Please if someone can help. I am new to batch scripting.
Any help would be appreciated.
Thanks
回答1:
Here is another way using a batch/vbs hybrid script.
@echo off
setlocal
call :FindReplace "Buildnumber=0" "Buildnumber=1.21" abc.properties
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
回答2:
You can do it pretty easily with sed
#!/bin/bash
BUILD="1.1"
sed "s/^BuildNumber=.*/BuildNumber=$BUILD/" abc.properties
This assumes that there are no spaces between BuildNumber and the = sign, otherwise you can use this one :
sed "s/^\(BuildNumber\s*=\s*\).*$/\1$BUILD/" abc.properties
来源:https://stackoverflow.com/questions/20034862/replace-a-number-in-properties-file-through-batch-file