Errorlevel batch command returns 0

I am trying to create a batch file that executes another “select” command based on an executable version of Windows. The syntax for the select command is different from Windows 7 and Windows XP.

The select command returns 1 for Y and 2 for N. The following command returns the correct error level:

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 

However, when it is combined with a command to detect a version of Windows, errorlevel returns 0 before AN after the select command in my Windows XP and Windows 7 code blocks.

 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. ) 

As you can see, I even tried to clear varlevellevel before executing the select command, but the error level remains at 0 after executing the select command.

Any tips? Thanks!

+6
source share
1 answer

You are faced with a classic problem - you are trying to expand %errorlevel% in parentheses in parentheses. This form of expansion occurs during parsing, but the entire IF construct is parsed immediately, so the %errorlevel% value will not change.

The solution is simple - with delayed expansion. You need SETLOCAL EnableDelayedExpansion at the top and then use !errorlevel! . Deferred expansion occurs at run time, so you can see changes in value in parentheses.

The help for SET ( SET /? ) Describes the problem and solution regarding the FOR statement, but the concept is the same.

You have other options.

You can move the code from the IF body to the marked sections of the code without parentheses and use GOTO or CALL to access the code. Then you can use %errorlevel% . I don't like this parameter because CALL and GOTO relatively slow and the code is less elegant.

Another option is to use IF ERRORLEVEL N instead of IF !ERRORLEVEL!==N (See IF /? ) Since IF ERRORLEVEL N checks if the error level is> = N, you need to run your tests in descending order.

 REM Windows XP ver | findstr /i "5\.1\." > nul if '%errorlevel%'=='0' ( choice /t:Y,5 "Do you want to automatically shutdown the computer afterwards " if ERRORLEVEL 2 set Shutdown=F if ERRORLEVEL 1 set Shutdown=T ) 
+13
source

Source: https://habr.com/ru/post/904352/


All Articles