Solved

batch file 'choice' + if errorlevel logic not working

Posted on 2014-03-19
2
917 Views
Last Modified: 2014-03-19
I am not getting the desired results, and I'm not sure what I'm doing wrong with this simple code.  Hopefully it's clear what I'm trying to do.  I did reading on the subject but failed and have wasted enough time so I am hoping an expert can help.

This is to run on Server 2008 R2.
choice /C 123456780 /N /M "Type a number or close the window to cancel:"

if ERRORLEVEL 9 (
do stuff
goto end
)

if ERRORLEVEL 8 (
do stuff
goto end
)

:end

Open in new window

It continues 9 through 1, but doesn't match so goes to :end
I also tried a block of
if errorlevel 1 goto 1
if errorlevel 2 goto 2

1:
stuff
goto end

2:
stuff
goto end

:end

Open in new window

What I found is that I can echo %errorlevel% and get the proper value returned from choice, but if I try to do anything else it becomes null or 0, not sure.
0
Comment
Question by:gnivler
2 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39940591
ERRORLEVEL is reset after almost every command, so after choice, capture ERRORLEVEL into a local-variable (set-command), and use the local-variable in further if-expressions.
0
 
LVL 2

Author Closing Comment

by:gnivler
ID: 39940645
Thanks for the answer.  I had tried set answer = %ERRORLEVEL% earlier but that doesn't seem to work.  From other sources, I just tried set /a and got the desired results.

echo %ERRORLEVEL%
set answer = %ERRORLEVEL%
echo %answer%

yields:

Type a number or close the window to cancel: 4
4
ECHO is off.

with set /a

Type a number or close the window to cancel: 4
4
4
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question