-3

I'm trying to make a batch file-based game, and i want to be able to collect user input. It should be a bit like the pause command, but asks for input from the user.

I don't have much code yet, because this is at the start of a program, but heres what I have so far:

@echo off echo Start the game? [Y/N] 

Here's what the output should look like:

Start the game? [Y/N] y 

Does anyone know what I can do to solve this?

3

1 Answer 1

-1

For simple yes/no selection use choice then test errorlevel - for example:

choice /N /M "Start the game? [Y/N] " if %errorlevel%==1 goto start else goto end :start :: Do something here :end :: Finish here 

Run choice /? for additional options. Note that choice displays the options and the ? automatically like:

Start the game [Y,N]? 

To get the exact text you suggested, supress the options and put them in the prompt:

choice /N /M "Start the game? [Y/N] " 

For more complex user input you can set an environment variable from a prompted user input using syntax:

set /P <environment variable name>=<prompt> 

then you test the environment variable string however you require.

For example, although overkill for single character y/n entries where choice is the simpler option:

@echo off :: Get input :input_start_yn set /P ANSWER=Start the game? [Y/N] %=% if "%ANSWER%"=="Y" goto start if "%ANSWER%"=="y" goto start if "%ANSWER%"=="N" goto end if "%ANSWER%"=="n" goto end goto input_start_yn :start :: Do something here :end :: Finish here 

The environment variable solution has the advantage of storing user input permanently, whereas errorlevel is temporary and will be overwritten by subsequent commands. It also allows string entry rather then single characters, so can be used for more complex input.

Sign up to request clarification or add additional context in comments.

5 Comments

Just out of curiosity, why set /P <environment variable name>=<prompt> %=% and not the more standard set /P "<environment variable name>=<prompt> "?
@SomethingDark good question. I have no idea! I likely copied it from some example years ago and have been duplicating it everywhere since without question. I'll see if I can find out where it came from and why.
@SomethingDark - Nope, can't find it. A bit "cargo cult" of me, duplicating something useless without understanding why. If I recall where that came from, I'll update, but meanwhile deleting it.
the reasoning would probably be "a definitively empty variable (as you can not define a variable name with a =) to make the trailing space obvious" - where quotes do a much better job (as there could be spaces after %=%, which you don't easily spot; Possible spaces after the closing quote don't hurt, as they become not part of the value - spaces after %=% will.
I think it is very specific to the script I copied it from which was written long ago.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.