The third FOR statement in the code below is supposed to return '18066' from the command output from 'java full version "1.8.0_66-b18" but it is returning blank, which causes the comparison that follows to fail. Can anyone offer some help?
@echo off
set MSIFileName=jre1.8.0_66.msi
set KeyName="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe"
for /f tokens^=2-5^ delims^=e._m^" %%a in ("%MSIFilename%") do set VersionToInstall=%%a%%b%%c%%d
for /f "tokens=2*" %%h in ('reg query %KeyName% /v Path') do set JavaHome=%%i
PATH "%PATH%;%JavaHome%"
for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set InstalledVersion=%%j%%k%%l%%m
if "%InstalledVersion%."=="." goto INSTALL
if "%InstalledVersion%" GEQ "%VersionToInstall%" goto END
TIA for the replies.
Note: I borrowed some code from user npocmaka found here.
-EDIT-
I still don't have an answer for why the original code didn't work but I found a workaround that has my script working. Rather than adding JavaHome to system PATH so the script could find java.exe I used 'cd' instead. In hope that this helps someone else here's the updated code...
@echo off
set MSIFileName=jre1.8.0_66.msi
set KeyName="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe"
set JavaHome=0
set IsNumber=0
for /f tokens^=2-5^ delims^=e._^" %%a in ("%MSIFilename%") do set VersionToInstall=%%a%%b%%c%%d
for /f "tokens=2*" %%e in ('reg query %KeyName% /v Path') do set JavaHome="%%f"
if %JavaHome% EQU 0 goto INSTALL
cd %JavaHome%
for /f tokens^=2-5^ delims^=._-^" %%g in ('java -fullversion 2^>^&1') do set InstalledVersion=%%g%%h%%i%%j
for /f "tokens=* delims=0123456789" %%k in ("%InstalledVersion%") do (
if "[%%k]" EQU "[]" set IsNumber=1
)
if %IsNumber% EQU 0 goto INSTALL
if "%InstalledVersion%" GEQ "%VersionToInstall%" goto END