合并这两个批量查询?

时间:2013-08-16 14:28:10

标签: java windows batch-file cmd

我想合并以下批处理文件。必须有一种更简单的方法来检测当前安装的Java版本,如果是最新的goto End,如果没有安装最新版本。

我很乐意参加for / f命令并在线发现:

@echo off
setlocal

set VERSION7="1.7.0_25"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
@echo Output: %%g
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
@echo Output: %JAVAVER%

for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
@echo Major: %%v
@echo Minor: %%w
@echo Build: %%x
)
endlocal

我目前安装Java运行时的脚本是:

IF exist %windir%\gpologs\jre_1.7.21.x86.txt ( goto eof ) ELSE ( goto Q1 )

:Q1 
Reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F86417025FF}" 
if "%ErrorLevel%"=="0" ( goto eof ) ELSE ( goto Q2 )

:Q2 
Reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F83217025FF}" 
if "%ErrorLevel%"=="0" ( goto eof ) ELSE ( goto Q3 ) 

:Q3 
Reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F83217021FF}" 
if "%ErrorLevel%"=="0" ( goto eof ) ELSE ( goto Install ) 

:Install 
msiexec /i "\\Sharename\GPOInstalls\Java\Java 1.7.0_21 x86\jre1.7.0_21.msi" /qn
if %ErrorLevel% EQU 0 (
    >>"\Sharename\gpolog\jre_1.7.21.x86.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Java Runtime 1.7.0_21x86 Installed"
    >>"%windir%\GpoLogs\jre_1.7.21.x86.txt" echo "Java Runtime 1.7.0_21x86 Installed"
) else (
    >>"\\Sharename\gpolog\JavaInstallErrors.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Error trying to install Java 1.7_21x86"
)

:eof 

我如何使用第一个代码(for / f)来搜索java版本,如果它不符合最新版本,请转到我的代码的第二部分:安装部分?

我不知道如何在for / f部分中包含它。

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

我想建议使用wget for Windowsgrep for Windows的解决方案。说明在评论中:

@ECHO OFF &SETLOCAL

REM get the latest Java version from the Oracle web site
FOR /f "tokens=3" %%a IN ('wget -O- "http://www.oracle.com/technetwork/java/javase/7u-relnotes-515228.html" 2^>nul^|grep -io "Changes in [^<]*"') DO (
    IF NOT DEFINED JAVA_latest SET "JAVA_latest=%%~a"
)

REM get the current installed Java version
for /f "tokens=3" %%a in ('java -version 2^>^&1 ^| find /i "version"') do SET "JAVA_current=%%~a"

REM print the results
ECHO Java versions: latest: %JAVA_latest%, current installed: %JAVA_current%

REM compare the versions, exit if equal
IF "%JAVA_latest%"=="%JAVA_current%" (ECHO Latest Java version already installed.&GOTO:eof)

REM put your install code here
ECHO here goes your code