1

我正在使用以下内容输出从另一个站点获取的 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?请尽快解释一下,这会更有意义:)

4

2 回答 2

3

默认分隔符是空格和制表符

您计算字符串中的“单词”并使用空格/制表符作为分隔符,您的第三个标记/单词是"1.7.0_25"

于 2013-10-02T11:06:50.917 回答
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"
)
于 2013-10-21T14:54:42.620 回答