?
Solved

batch file 'choice' + if errorlevel logic not working

Posted on 2014-03-19
2
Medium Priority
?
979 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 1500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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