Batch Script to Update text in the properties file

与世无争的帅哥 提交于 2019-12-13 03:54:11

问题


I have a question regarding the batch.

I have a properties file in Directory. Which has a text "BuildNumber=0". I want to replace this "BuildNumber=0" with "BuildNumber=%BUILD_NUMBER%". I am using the following script to achieve this

@echo on
setlocal enabledelayedexpansion enableextensions


ATTRIB -r -s C:\bldarea\myfile\..\..\jenkinstest\abc.properties

CD C:\bldarea\myfile\file\Main_Releases\jenkinstest\
call :FindReplace "Buildnumber=0" "Buildnumber=%BUILD_NUMBER%" abc.properties
exit /b 

:FindReplace <Buildnumber=0> <Buildnumber=%BUILD_NUMBER%> <abc.properties>
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

The problem with this script is , It is not converting BuildNumber=0 to current Build Number.

From the line in code:

:FindReplace <Buildnumber=0> <Buildnumber=%BUILD_NUMBER%> <abc.properties>

if I remove % symbol then it is printing the "BuildNumber=%BUILD_NUMBER%" but the "BuildNumber=0" is still present in the doc. Can someone please help me out to replace the text correctly.

Thanks.


回答1:


Just insert double percents in this line:

call :FindReplace "Buildnumber=0" "Buildnumber=%BUILD_NUMBER%" abc.properties

this way:

call :FindReplace "Buildnumber=0" "Buildnumber=%%BUILD_NUMBER%%" abc.properties


来源:https://stackoverflow.com/questions/20144116/batch-script-to-update-text-in-the-properties-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!