Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / How can I make a Choice in a batch file?

Author
Message
JohannesM
18
Years of Service
User Offline
Joined: 22nd Nov 2005
Location: Espoo, Finland
Posted: 1st Jun 2007 20:20
Hey guys!

I started programming on batch files (Dont say things like:
Quote: "OMG .BAT IS STOOPID"
to that please ) and I would like to know how to make a choice between 2 options. I'm making an adventure game and I want there to be this kind of choice:

You press 1 and then, it links you to a part of the code. Then if you press 2, it links you to another part of the code.

How can I do this?

- JM

www.freewebs.com/supahsfpscstuff THE FORUM IS UP! JOIN NOW AND GET UNREALEASED MODEL PACK! Space open for first 3 joiners.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Jun 2007 20:33 Edited at: 1st Jun 2007 20:34
CHOICE
Description The CHOICE command is perhaps the best addition to MS DOS Batch File commands. CHOICE makes it possible to accept various user-responses. Before now, users were presented with crude either/or choices in batch files. The CHOICE command allows a batch file to detect a users choice from a lits of options.
Syntax CHOICE [/C:choices] [/N] [/S] [/T:choice,timeout] [TEXT]
Where -:

/C:choices : specifies the choices that the user can choose from. The choices can only be single characters.
/N : Do not display choices and the '?' at the end of the TEXT prompt.
/S : Treat the choices as case sensitive, meaning that 'a' is a different choice from 'A'. By default, case is not sensitive - 'a' is equivalent to 'A'.
/T:choice,timeout : Default to choice after timeout seconds.
TEXT : The text to display as the prompt of the choice.

Typical Use The CHOICE command has its obvious use in batch files. It is now possible to easily get a users response, thus allowing batch files to be much more interactive, and therefore more useful.

Example The following batch file snippet displays a simple menu (without a question-mark at the end of the prompt) and prompts for the users choice, defaulting to option 2 after 5 seconds :
ECHO 1. MS-DOS Editor.
ECHO 2. MS-Windows. (default)
ECHO 3. Defrag the hard-drive.
ECHO 4. Quit.
CHOICE /C:1234 /N /T:2,5 Please choose a menu option.
IF ERRORLEVEL == 4 GOTO QUIT_MENU
IF ERRORLEVEL == 3 GOTO DEFRAG_HD
IF ERRORLEVEL == 2 GOTO RUN_WIN
IF ERRORLEVEL == 1 GOTO RUN_EDIT
:RUN_EDIT
CALL EDIT
:RUN_WIN
CALL WIN
:dEFRAG_HD
DEFRAG c:
:QUIT_MENU
ECHO Safe to switch off machine now...

Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 1st Jun 2007 21:09
Would seriously recommend if you're doing this in Windows, that you use VBScript instead of a batch file. You get more control over what your code does, and the system.

Login to post a reply

Server time is: 2024-11-18 17:33:10
Your offset time is: 2024-11-18 17:33:10