Solved

batch file 'choice' + if errorlevel logic not working

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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 …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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