После расширения переменной строка:
for /f "tokens=*" %%v in ('%java_cmd% 2^>^&1 ^| find "version"') do echo %%v
расширяется до чего-то вроде:
for /f "tokens=*" %%v in ('"C:\Program Files (x86)\...\java.exe" 2^>^&1 ^| find "version"') do echo %%v
, поэтому командная строка, которая должна выполняться for /F
, начинается и заканчивается "
.
for /F
использует cmd /c
для выполнения этой командной строки, которая удаляет окружающий "
, оставляя это позади:
C:\Program Files (x86)\...\java.exe" 2>&1 | find "version
, что, очевидно, является неправильным синтаксисом.
Чтобы избежать этого, измените командную строку так, чтобы она не была заключена в кавычки (путем перемещения части перенаправления здесь):
for /f "tokens=*" %%v in ('2^>^&1 %java_cmd% ^| find "version"') do echo %%v
В качестве альтернативы, предоставьте явные кавычки, которые могут быть удалены с помощью cmd /C
, поэтому остаток остается в силе (избегайте этих кавычек, чтобы не пришлось изменять другие escape-последовательности):
for /f "tokens=*" %%v in ('^"%java_cmd% 2^>^&1 ^| find "version"^"') do echo %%v
Кроме того, я хотел бы предложить изменить весь сценарий бит (см. все замечания rem
):
@echo off
rem /* Begin the script with delayed expansion disabled as it might lead to problems
rem during expansion of `for` meta-variables in case the values contain `!`: */
setlocal EnableExtensions DisableDelayedExpansion
rem // Use quoted `set` syntax (this requires the command extensions to be enabled):
set /A "count=0"
for /F "tokens=*" %%j in ('where java') do (
set /A "count+=1"
rem // Enable delayed expansion only when needed, like for variable `count` here:
setlocal EnableDelayedExpansion
rem /* (Mis-)use a `for` loop to make the value of `!count!` available even when
rem delayed expansion is disabled, hence after `endlocal` then: */
for %%c in (!count!) do endlocal & set "JAVA[%%c]=%%j"
rem /* Pass an argument over to the sub-routine rather than using variables
rem globally; in this situation this even does not need delayed expansion: */
call :ECHO_JAVA "%%j"
)
endlocal
goto :EOF
:ECHO_JAVA
rem /* Explicitly disable delayed expansion in the sub-routine, so you may call
rem it even from a code section with delayed expansion enabled: */
setlocal DisableDelayedExpansion
rem /* Use the sub-routine argument (`%~1`) herein, with explicit control of
rem quotation (`~` removes potential quotes, then we explicitly add such): */
for /F "tokens=*" %%v in ('2^>^&1 "%~1" -version ^| find "version"') do echo(%%v
endlocal
exit /B 0