Solved

batch file 'choice' + if errorlevel logic not working

Posted on 2014-03-19
2
899 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

9 Experts available now in Live!

Get 1:1 Help Now