0

I'm working on a small batch script, which includes a section similar to the code block below... it would be an understatement to say this has befuddled me to the point where my mind has gone completely numb... why on gaias green backside does this not work...?

@echo off

set log=0
choice /m "Choose "
if errorlevel 2 set log=N
if errorlevel 1 set log=Y

echo %log%
pause

if "%log%"=="N" (
echo hello
)


if "%log%"=="Y" (
echo goodbye
)
pause
dan1st
  • 12,568
  • 8
  • 34
  • 67
Albert F D
  • 529
  • 2
  • 13
  • 33

2 Answers2

1
if errorlevel 2 set log=N
if errorlevel 1 set log=Y

translated:

if errorlevel is 2 or greater than 2 set log=N
if errorlevel is 1 or greater than 1 set log=Y

so - reverse the lines since if errorlevel is 2, it is both 2 or greater than 2 (so set N) and then is 1 or greater than 1 (so set Y)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Defies standard convention for choice command syntax but It makes sense now that you've explained it. Thank you very much indeed! – Albert F D Feb 06 '15 at 18:01
1

An oddity within Windows (and old DOS) is that if you set "if errorlevel ..." it actually means "if the errorlevel is greater than this number..." So if you say "if errorlevel 1" you mean "if errorlevel > 1".

Try this:

if errorlevel 1 if not errorlevel 2 (do stuff)

if errorlevel 2 if not errorlevel 3 (do other stuff)

Alternatively, you can use the temporary variable %ERRORLEVEL%...

theglossy1
  • 543
  • 3
  • 13