我使用以下内容输出我从其他网站抓取的java版本:
java -version 2> java.txt
for /f "tokens=3" %%x in ('find /i "java version" java.txt')
输出是:
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
以下是完整代码:
@echo off
set JavaTemp=C:\Windows\Gpologs
java -version
if "%ErrorLevel%"=="0" ( goto VersionCheck ) ELSE ( goto Install )
:VersionCheck
java -version 2> %JavaTemp%\java.txt
for /f "tokens=3" %%x in ('find /i "java version" %JavaTemp%\java.txt') do (
if %%~x==1.7.0_45 goto :eof
if %%~x==1.7.0_40 goto Install
if %%~x==1.7.0_25 goto Install
if %%~x==1.7.0_21 goto Install
if %%~x==1.7.0_17 goto Install
if %%~x==1.7.0_15 goto Install
)
:Install
msiexec /i "\\servershare\sharename\Java\jre1.7.0_45_x86\jre1.7.0_45.msi" /qn
if %ErrorLevel% EQU 0 (
>>"\\servershare\sharename\jre_1.7.45.x64.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Java Runtime 1.7.0_45x86 Installed"
>>"%windir%\GpoLogs\jre_1.7.45.x64.txt" echo "Java Runtime 1.7.0_45x86 Installed"
) else (
>>"\\servershare\sharename\JavaInstallErrors.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Error trying to install Java 1.7_45x86"
)
我只需要知道为什么令牌是3?请尽快解释一下,它会更有意义:)
答案 0 :(得分:3)
默认分隔符是空格和TAB
你计算字符串中的'words'并使用空格/制表符作为分隔符,你的第3个标记/单词是"1.7.0_25"
答案 1 :(得分:1)
Foxidrive回答了你的问题。
与您的问题无关,您的代码可以通过完全相同的结果大大简化:
@echo off
for /f "tokens=3" %%V in (
'java -version 2^>^&1 ^| find /i "java version"'
) do if %%~V==1.7.0_45 exit /b
msiexec /i "\\servershare\sharename\Java\jre1.7.0_45_x86\jre1.7.0_45.msi" /qn
if %ErrorLevel% EQU 0 (
>>"\\servershare\sharename\jre_1.7.45.x64.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Java Runtime 1.7.0_45x86 Installed"
>>"%windir%\GpoLogs\jre_1.7.45.x64.txt" echo "Java Runtime 1.7.0_45x86 Installed"
) else (
>>"\\servershare\sharename\JavaInstallErrors.csv" echo "%computername%","%date%","%Time%","%ErrorLevel%","Error trying to install Java 1.7_45x86"
)