Sto cercando di creare un file batch che esegua un comando 'choice' diverso in base alla versione di Windows in esecuzione. La sintassi del comando choice è diversa tra Windows 7 e Windows XP.Il file di errore del file batch 'choice' restituisce 0
Scelta comando restituisce un 1 per Y e 2 per N. Il comando seguente restituisce il corretto livello di errore:
Windows 7:
choice /t 5 /d Y /m "Do you want to automatically shutdown the computer afterwards "
echo %errorlevel%
if '%errorlevel%'=='1' set Shutdown=T
if '%errorlevel%'=='2' set Shutdown=F
Windows XP:
choice /t:Y,5 "Do you want to automatically shutdown the computer afterwards "
echo %ERRORLEVEL%
if '%ERRORLEVEL%'=='1' set Shutdown=T
if '%ERRORLEVEL%'=='2' set Shutdown=F
Tuttavia , quando è combinato con un comando per rilevare la versione del sistema operativo Windows, errorlevel restituisce 0 prima di AN dopo il comando choice in entrambi i miei blocchi di codice Windows XP e Windows 7.
REM Windows XP
ver | findstr /i "5\.1\." > nul
if '%errorlevel%'=='0' (
set errorlevel=''
echo %errorlevel%
choice /t:Y,5 "Do you want to automatically shutdown the computer afterwards "
echo %ERRORLEVEL%
if '%ERRORLEVEL%'=='1' set Shutdown=T
if '%ERRORLEVEL%'=='2' set Shutdown=F
echo.
)
REM Windows 7
ver | findstr /i "6\.1\." > nul
if '%errorlevel%'=='0' (
set errorlevel=''
echo %errorlevel%
choice /t 5 /d Y /m "Do you want to automatically shutdown the computer afterwards "
echo %errorlevel%
if '%errorlevel%'=='1' set Shutdown=T
if '%errorlevel%'=='2' set Shutdown=F
echo.
)
Come potete vedere, ho anche provato aprendo l'errorlevel var prima di eseguire il comando di scelta, ma errorlevel rimane a 0 dopo l'esecuzione del comando di scelta.
Qualche consiglio? Grazie!