0

I have a properties file build.properties with following information :

conf.major.number=1
conf.minor.number=0
core.major.number=3
core.minor.number=2

And I want my batch script to fetch the values from conf.major.number and conf.minor.number and result the output as 1.0 and set it to a variable "version"

and when I give echo %version% the output shoud be 1.0

for /f "skip=1 delims== tokens=2" %%a in (build_info.properties) DO (set "version=%%a")
echo %version%

core.major.number=1
core.minor.number=0

The above lines should be ignored and I dont know how to skip reading those lines.

Kindly help,

Thanks

Here is the solution and it works perfectly thanks @JosefZ

@ECHO OFF SETLOCAL EnableExtensions EnableDelayedExpansion
set VER_FILE="tldconfrev.number"
set /p id=Enter Ant target to be called "revision" or "hi": 
set /p flag=Do you want the version file to be updated to perforce (Y/N): 
echo(%id%
if /I "%id%"=="revision" (
  if /I "%flag%"=="Y" (
    for /f "delims=" %%a in (
    'type "build_info.properties"^|find "="'
  ) DO (
  set "_%%a"
)
set _
set "version=!_conf.major.number!.!_conf.minor.number!"
echo version=!version!
pause
)
)
sanam_bl
  • 299
  • 1
  • 7
  • 14

2 Answers2

1

Try next approach:

@ECHO OFF
SETLOCAL EnableExtensions
for /f "delims=" %%a in (
      'type "D:\bat\files\build_info.properties"^|find "="'
    ) DO (
  set "_%%a"
)
set _
set "version=%_conf.major.number%.%_conf.minor.number%"
echo version=%version%

Output:

==>D:\bat\SO\32266650.bat
_conf.major.number=1
_conf.minor.number=0
_core.major.number=3
_core.minor.number=2
version=1.0

==>

Edit with respect to your comment (next, edit your question and add the code there, please):

@ECHO OFF SETLOCAL EnableExtensions EnableDelayedExpansion
set VER_FILE="tldconfrev.number"
set /p id=Enter Ant target to be called "revision" or "hi": 
set /p flag=Do you want the version file to be updated to perforce (Y/N): 
echo(%id%
if /I "%id%"=="revision" (
  if /I "%flag%"=="Y" (
    for /f "delims=" %%a in (
        'type "build_info.properties"^|find "="'
      ) DO (
      set "_%%a"
    )
    set _
    set "version=!_conf.major.number!.!_conf.minor.number!"
    echo version=!version!
    pause
  )
)

Resources (required reading):

Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • to keep it shorter (I assume there are more than those four lines in the file), you could use `find "conf.m"` instead of `find "="` – Stephan Aug 28 '15 at 09:33
  • @JosefZ, your code works fine but when I implement the same thing in my code it does not show the version – sanam_bl Aug 28 '15 at 10:02
  • @ECHO OFF SETLOCAL EnableExtensions set VER_FILE="tldconfrev.number" set /p id=Enter Ant target to be called "revision" or "hi": set /p flag=Do you want the version file to be updated to perforce (Y/N): echo %id% if /I %id%==revision ( if /I %flag%==Y ( for /f "delims=" %%a in ( 'type "build_info.properties"^|find "="' ) DO ( set "_%%a" ) set _ set "version=%_conf.major.number%.%_conf.minor.number%" echo version=%version% pause ) ) ) – sanam_bl Aug 28 '15 at 10:05
  • Sorry I dont know how to post the code in the right way. The above code can be directly copy pasted to try it out. – sanam_bl Aug 28 '15 at 10:06
  • you fell into the [delayed expansion trap](http://stackoverflow.com/a/30284028/2152082) – Stephan Aug 28 '15 at 11:45
  • Any suggestion to fix this? – sanam_bl Aug 28 '15 at 11:56
  • @Stephan I don't agree fairly. We need `"="` to perform `set "_%%a"` command properly. However, `^|find "conf.m"^|find "="` could narrow number of `_variables` – JosefZ Aug 28 '15 at 13:31
  • @JosefZ: depends on the file content. Nearly idiot-proof would be `findstr /b "conf\.m..or\.number="` – Stephan Aug 28 '15 at 19:19
0

There's more than one way to skin a cat - here are two more:

A:

@echo off
for /f "usebackq delims=" %%a in ("build_info.properties") do set %%a
echo Version is "%conf.major.number%.%conf.minor.number%"
pause

B:

@echo off
<build_info.properties (
set /p maj=
set /p min=
)
set %maj%
set %min%
echo Version is "%conf.major.number%.%conf.minor.number%"
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68