[SOLVED]CHOICE ERRORLEVEL problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

[SOLVED]CHOICE ERRORLEVEL problem

#1 Post by jwoegerbauer » 13 Mar 2015 09:02

MOD EDIT: See later posts as this was not a problem with choice as mentioned incorrectly here:

Often the ERRORLEVEL returned in a batch file from CHOICE was incorrect. Mainly if CHOICE wasn't within an IF and/or FOR block. It took me hours to find the solution to get the correct ERRORLEVEL returned: Always wrap CHOICE into a FOR block.

Example:

@ECHO OFF
SETLOCAL enableDelayedExpansion

....

CALL :askForTerminatePrograms

....

ENDLOCAL
EXIT /b

:askForTerminatePrograms
CLS
ECHO.
ECHO PLEASE TERMINATE ALL RUNNING ANTIVIRUS / ANTIMALWARE / ANTISPYWARE AND ANY OTHER
ECHO INTERNET SECURITY RELATED PROGRAMS!
ECHO.
FOR /l %%? IN (1,1,1) DO (
CHOICE /M "All running Antivirus /Antimalware / Antispyware programs terminated "
SET /a answer=!ERRORLEVEL!
IF !answer! EQU 2 (
CHOICE /M "Do you want to run TaskManager to manually stop these programs "
SET /a answer=!ERRORLEVEL!
IF !answer! EQU 2 GOTO :NO_TASK
START "" /wait taskmgr
:NO_TASK
)
SET "answer="
)
GOTO :EOF


Thought I should share this.
Last edited by jwoegerbauer on 14 Mar 2015 23:11, edited 1 time in total.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: [SOLVED] CHOICE ERRORLEVEL problem

#2 Post by ShadowThief » 13 Mar 2015 09:32

Please give an example of your other code where choice was returning an incorrect errorlevel value, because I absolutely guarantee you were doing something wrong.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: [SOLVED] CHOICE ERRORLEVEL problem

#3 Post by Squashman » 13 Mar 2015 09:52

ShadowThief wrote:Please give an example of your other code where choice was returning an incorrect errorlevel value, because I absolutely guarantee you were doing something wrong.

Ditto!

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: [SOLVED] CHOICE ERRORLEVEL problem

#4 Post by jwoegerbauer » 13 Mar 2015 10:07

This was the code I used before - it most times returned an ERRORLEVEL equal 0 although "N" was selected, which should result in an ERRORLEVEL equal 2


CLS
ECHO.
ECHO.
ECHO PLEASE TERMINATE ALL RUNNING ANTIVIRUS / ANTIMALWARE / ANTISPYWARE AND ANY OTHER
ECHO INTERNET SECURITY RELATED PROGRAMS! ALSO TURN WIFI OFF.
ECHO.
CHOICE /M "All running Antivirus /Antimalware programs terminated and WiFi turned off"
IF ERRORLEVEL 2 (
CHOICE /M "Do you want to run TaskManager to stop all running Antivirus /Antimalware programs"
IF ERRORLEVEL 2 GOTO :END
START "" /wait taskmgr
)

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: [SOLVED] CHOICE ERRORLEVEL problem

#5 Post by ShadowThief » 13 Mar 2015 10:41

With that exact code you've posted, it's not going to work because there is no label called "end." If you want to skip to the end of the script with a goto, you can say goto :eof instead; :eof being a built-in label that represents the end of the file. You could also say exit /b if you wanted to quit the script right then and there.

That said, once you change goto :end to goto :eof, the script seems to work fine.

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: [SOLVED] CHOICE ERRORLEVEL problem

#6 Post by jwoegerbauer » 13 Mar 2015 11:37

ShadowThief,

this was the problem

This was the code I used before - it most times returned an ERRORLEVEL equal 0 although "N" was selected, which should result in an ERRORLEVEL equal 2



and not the GOTO command, or something else.


All,

do not spend more time in this, because, as said, for me it's SOLVED. The 1000-lines batch file the code shown in post #1 is derived of, works perfectly.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: [SOLVED] CHOICE ERRORLEVEL problem

#7 Post by ShadowThief » 13 Mar 2015 11:46

I'm just trying to figure out why you would have an errorlevel of 0. It should literally not be possible when being returned from a choice command when you check the value the way you have. If you had used %errorlevel% instead, it would be because you needed delayed expansion inside of a code block, but that's not the case here.

Add that to the fact that the sample code you provided does not create the error (and you're the only person I've ever heard of even having this error), and I'm left very confused and determined to find a cause of this behavior.
Last edited by ShadowThief on 13 Mar 2015 13:05, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: [SOLVED] CHOICE ERRORLEVEL problem

#8 Post by Squashman » 13 Mar 2015 12:56

jwoegerbauer wrote:
All,

do not spend more time in this, because, as said, for me it's SOLVED. The 1000-lines batch file the code shown in post #1 is derived of, works perfectly.

Then starting this thread was pointless because you cannot prove your hypothesis. I don't want other people stumbling upon this thread thinking this is the only way to do this. If you can't prove your logic then we might as well delete this thread.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: [SOLVED] CHOICE ERRORLEVEL problem

#9 Post by penpen » 13 Mar 2015 13:46

@jwoegerbauer:
I bet you have checked the errorlevel this way (i have never seen another cause on that "issue").

Code: Select all

@echo off
choice /M "Text"
if ErrorLevel 0 (
   echo errorlevel == 0
) else if ErrorLevel 1 (
   echo errorlevel == 1
) else if ErrorLevel 2 (
   echo errorlevel == 2
)
If i am right, then you should reread "if /?" to solve this issue.
Using for won't fix it.

penpen

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: [SOLVED] I used a goto incorrectly, not CHOICE ERRORLEVE

#10 Post by jwoegerbauer » 14 Mar 2015 12:05

MOD EDIT: See later posts as this was not a problem with choice as mentioned incorrectly here

So you think I'm insane. Then so be it.

I took the the time to write a test .BAT with content as follows

Code: Select all

@echo off
SETLOCAL enabledelayedexpansion

ECHO.
ECHO CHOICE being located in an IF block and SETLOCAL enabledelayedexpansion
ECHO.

IF "1" EQU "1" (

rem 1st
CHOICE /M "Continue "
ECHO answer %ERRORLEVEL%

rem 2nd
CHOICE /M "Continue "
ECHO answer !ERRORLEVEL!

rem 3rd
CHOICE /M "Continue "
IF ERRORLEVEL 2 (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

rem 4th
CHOICE /M "Continue "
IF "%ERRORLEVEL%"=="2" (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

)

TIMEOUT 3

ECHO.
ECHO CHOICE  being located in a FOR loop and SETLOCAL enabledelayedexpansion
ECHO.

FOR /l  %%? IN (1,1,1) DO (

rem 1st
CHOICE /M "Continue "
ECHO answer %ERRORLEVEL%

rem 2nd
CHOICE /M "Continue "
ECHO answer !ERRORLEVEL!

rem 3rd
CHOICE /M "Continue "
IF ERRORLEVEL 2 (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

rem 4th
CHOICE /M "Continue "
IF "%ERRORLEVEL%"=="2" (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

)

TIMEOUT 3

ECHO.
ECHO CHOICE not being located in an IF block and SETLOCAL enabledelayedexpansion
ECHO.

rem 1st
CHOICE /M "Continue "
ECHO answer %ERRORLEVEL%

rem 2nd
CHOICE /M "Continue "
ECHO answer !ERRORLEVEL!

rem 3rd
CHOICE /M "Continue "
IF ERRORLEVEL 2 (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

rem 4th
CHOICE /M "Continue "
IF "%ERRORLEVEL%"=="2" (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

TIMEOUT 3

ECHO.
ECHO CHOICE not being located in a FOR loop and SETLOCAL enabledelayedexpansion
ECHO.
rem 1st
CHOICE /M "Continue "
ECHO answer %ERRORLEVEL%

rem 2nd
CHOICE /M "Continue "
ECHO answer !ERRORLEVEL!

rem 3rd
CHOICE /M "Continue "
IF ERRORLEVEL 2 (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

rem 4th
CHOICE /M "Continue "
IF "%ERRORLEVEL%"=="2" (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

TIMEOUT 3

ENDLOCAL
SETLOCAL

ECHO.
ECHO CHOICE not being located in an IF block /  FOR loop and SETLOCAL
ECHO.
rem 1st
CHOICE /M "Continue "
ECHO answer %ERRORLEVEL%

rem 2nd
CHOICE /M "Continue "
ECHO answer !ERRORLEVEL!

rem 3rd
CHOICE /M "Continue "
IF ERRORLEVEL 2 (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

rem 4th
CHOICE /M "Continue "
IF "%ERRORLEVEL%"=="2" (
ECHO answer 2
) ELSE (
ECHO answer INCORRECT
)

PAUSE

ENDLOCAL
EXIT /b



to test on ERRORLEVEL returned if "N" key (NO) was pressed.

If you take the time to run it, you will see, that if CHOICE is located within an IF block / FOR loop and with 'SETLOCAL enabledelayedexpansion', then wrong results are returned. Same is the case if CHOICE is NOT is located within an IF block / FOR loop with only 'SETLOCAL'

Only if CHOICE is NOT located within an IF block / FOR loop but with 'SETLOCAL enabledelayedexpansion' then correct results are returned.

Have a nice day.
Last edited by jwoegerbauer on 14 Mar 2015 12:56, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: [SOLVED] I used a goto incorrectly, not CHOICE ERRORLEVE

#11 Post by penpen » 14 Mar 2015 12:53

That is neither an issue with choice nor with the errorlevel variable:
Command blocks are treated as one (complex) instruction.

The variables in the block (:=statement sequence within round brackets) are expanded before the first "inner" command is executed.
Sample code:

Code: Select all

set "var=abc"
(
   set "var=def"
   echo(var=%var%
)

The variable expansion transforms the above block into:

Code: Select all

set "var=abc"
(
   set "var=def"
   echo(var=abc
)

After that the block is executed:
- first the command line interpreter sets the environment variable "var" to "def",
- then "var=abc" is printed to screen.

penpen

Edit: Delayed expansion occurs ("immediately") before an atomic expression is executed.

Post Reply