2

Okay I am trying to ask the user the below question in a batch file but don't think that I am entering the correct choice command.

    echo Would you like to know the time? (Y/N)
    CHOICE /C YN /N
    GOTO OPTION-%ERRORLEVEL%

    :OPTION-Y  Yes 
    echo %time%
    goto cont

    :OPTION-N  No

    :cont

P.S today is my first day of the couse so I am a newbie, please don't judge.

Snoopy135
  • 21
  • 2
  • 2
    So you didn't bother to read the help for the `CHOICE` command. **The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on.** – Squashman Jan 31 '18 at 14:35

3 Answers3

2

Because %errorlevel% is a number not Y or N

Your labels should be :OPTION-1 and :OPTION-2:

@echo off
echo Would you like to know the time? (Y/N)
CHOICE /C YN /N
GOTO OPTION-%ERRORLEVEL%

:OPTION-1
echo %time%
goto cont

:OPTION-2

:cont

Here is another example so you can understand how it assigns the %errorlevel% number to the key you selected.

@echo off
:start
cls
CHOICE /C YNM /N /M "Should I display the Time? Select (Yes (Y)  No (N) or Maybe (M))"
if %errorlevel%==1 echo %time%
if %errorlevel%==2 echo Ok, I won't then
if %errorlevel%==3 echo it is fine, I will ask again in 10 seconds & timeout /T 10 & goto :start

Here you can see it assigns the first key to %errorlevel% 1, the second key to %errorlevel% 2 and third key to %errorlevel% 3 etc.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • So if I press Y on the keyboard, it will trigger option1? – Snoopy135 Jan 31 '18 at 12:32
  • @Snoopy135 Correct Yes. I tested already. just copy the code as is – Gerhard Jan 31 '18 at 12:33
  • 1
    @Gerhard, in your second example, `%DRIVE%` will always be assigned the value `A:`, you need to either reverse the `ERRORLEVEL`s from `5` to `1` or change the `IF` sequences to use `%ERRORLEVEL%` and `==`. – Compo Jan 31 '18 at 13:12
  • @compo, I did not actually see that as I got the example from robvanderwoude, but yes, I will fix it. – Gerhard Jan 31 '18 at 13:35
  • This will not work in a parenthesized block -- see https://stackoverflow.com/a/8616822/733092. – Noumenon Aug 26 '18 at 14:39
  • @noumenon, yes we know, but we do not need delayedexpansion here, so not really relevant here? – Gerhard Aug 26 '18 at 15:31
  • @GerhardBarnard I upvoted because this is a good answer to OP's question. I commented to help people who tried this answer and it inexplicably didn't work, like me. – Noumenon Aug 26 '18 at 16:33
  • 1
    @noumenon, Thanks for the upvote. I did not critisize in my comment. It was a reponse with a question.:) – Gerhard Aug 26 '18 at 16:45
0

CHOICE does not return the selected key as %ERRORLEVEL%, it returns the index of the selected key - that is, for CHOICE /C YN, if you select Y, %ERRORLEVEL% will be 1; for N, it will be 2. See SS64 on CHOICE.

You also have to be careful about the order that you test %ERRORLEVEL%; the standard construct IF ERRORLEVEL n ... is actually testing to see whether %ERRORLEVEL% is equal to or greater than n. See SS64 on ERRORLEVEL.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
0

You could also reduce all of your provided snippet to two lines, (continuing your script beneath them as necessary):

Choice /M "Would you like to know the time"
If Not ErrorLevel 2 Echo %TIME% & Timeout 3 >Nul
Compo
  • 36,585
  • 5
  • 27
  • 39
  • I think judging by his question, OP is doing an assignment and they probably have to use labels, but I could be wrong. – Gerhard Jan 31 '18 at 13:34