0

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

vicky
  • 29
  • 1
  • 3
  • 9

2 Answers2

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
bendaizer
  • 1,235
  • 9
  • 18
  • 1
    +1, sed would also be my suggestion for Windows user: [sed for Windows](http://gnuwin32.sourceforge.net/packages/sed.htm) – Endoro Nov 18 '13 at 01:10
  • +1 @endoro but I would use `repl.bat` instead of sed, for Windows. It's easier to install for the user (sed comes in multiple versions, some with dependencies), doesn't require admin permissions to install, and has good functionality. `repl.bat` from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat – foxidrive Nov 18 '13 at 01:43
  • Usually, when I have to work in windows environement, I simply go with cygwin to have all the unix utilities like sed, grep, awk, ssh ... Many people I know do the same ! It's very hard to achieve the same level of conciseness with windows based tools – bendaizer Nov 18 '13 at 13:21
  • Hey Bendaizer. I want to use batch only. Since my jenkins server is installed on windows 2k3 and all the files get stored on the same machine. It gets its file from the server and it builds the current file so please can u provide a work around in batch filer – vicky Nov 22 '13 at 13:58
0

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
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
  • Hey Matt thanks this is actually working for the property file. One more thing I just wanna ask. Can it just not replace the BuilNumber=1.21 and with current BuildNumber. – vicky Nov 18 '13 at 17:02
  • Yes, you can. Put whatever you want in the call line like this: `call :Findreplace "string to find" "string to replace it with" file` – Matt Williamson Nov 18 '13 at 17:06
  • Please a little more help from u @matt my build system get its data from the perforce and create a zip to a local repo. So if it can go to a particular path and then the file gets.updated. Thanks for all your help – vicky Nov 18 '13 at 18:04
  • Sorry Vicky, I don't quite understand. Can you explain in a little more detail exactly what you want to happen? – Matt Williamson Nov 18 '13 at 18:17
  • Hi Matt I am using jenkins server as my build system. Previously I was using BuildForge to build my projects. BuildForge had a logic in a BTM file which change the buildnumber of the my property file and create a zip file and move it to local repo. Now in jenkins there is only an option to Execute Windows Batch file, It does exactly same functionality but It doesn't update my property file. I want to locate the file through P4 sync the code and then create a zip. I have written some script but none of them are working. – vicky Nov 19 '13 at 12:04
  • Ok, I see. I'd recommend posting a new question. Include what you've tried so far in it and what program you are using to do the zipping. A full path to the local repo would be helpful too if you don't have it in your code. – Matt Williamson Nov 19 '13 at 12:08
  • Hi Matt I am using your script but the issue is It is not replacing the BuildNumber=0 with Current BuildNumber. Instead it is populating it as 0. I want to know is there any other way around I can do this. – vicky Nov 22 '13 at 11:23
  • Where are you getting the buildnumber from? Is it in a variable? – Matt Williamson Nov 22 '13 at 11:36
  • I ask a question Let me give you the Link I have posted your script there. Please have a look http://stackoverflow.com/questions/20144116/batch-script-to-update-text-in-the-properties-file – vicky Nov 22 '13 at 11:46