variablesbatch-filesetbatch-choice

Batch - Set variable through choice command


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

Solution

  • 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)