Solved

batch file 'choice' + if errorlevel logic not working

Posted on 2014-03-19
2
910 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now