In a batch file im messing with, I want the program to goto a certain section after pressing a certain key, such as D. Is there any way to do this after pressing a key without also having to press enter? I'm sorta a beginner.
This is possible using the choice command. This command will wait for a keypress, and if the key is in the list of keys after /c then it returns the offset of the key pressed in the list of keys.
choice /c AB /m "Press A or B"
if errorlevel 2 goto optionb
if errorlevel 1 goto optiona
You can also use the optional /n arguement to hide the list of options ([A,B] in this case) that appears after the message text.
Related
I am trying to find out how to make batch wait for a specific keystroke. I don't want any of those pause>nul echo Press Enter to continue... None of that stuff, I would like it so that way once you press a key (eg. a) then it will go to :Test, if you press another key then it won't react what so ever. And I looked at the first page result, and went to the links on Google.
I believe the solution to your problem is the CHOICE command. This will allow you to set a key, have the batch script wait for that key to be pressed, then continue. It also avoids any cryptic pausing or loops in the script.
Keep in mind that the choice command has a few syntax's
/C - Specifies the list of choices to be created.
/N - Hides the list of choices in the prompt.
/CS- Enables case-sensitive choices to be selected.
/T - The number of seconds to pause before a default choice is made.
/D - Specifies the default choice after timeout seconds.
/M - Specifies the message to be displayed before the prompt.
For the sake of your usage, the following script can be used. Its also important to note you can have multiple keys act as your specific keystroke by doing ex: 12345 for SET KEY=
#ECHO OFF
::Change this key to what ever you wish.
SET KEY=N
:Start
cls
echo Press %KEY% to continue...
choice /c %KEY% /n /cs
if %errorlevel%==1 goto Continue
:Continue
cls
echo Hello World!
pause.
This is a simple question that I haven't been able to find an answer to. I am running a script that requires user input/selection. Here is the problematic area:
echo W -- WATCH_SCALE
echo E -- EARRING_SCALE
echo R -- RING_SCALE
echo B -- BOX
set /P rnFunc="choose a script: "
for %%I in (W E R B x) do if #%rnFunc%==#%%I goto assign%%I
The script works if user inputs the correct letter, however if user inputs an undefined letter such as 'T' the script continues on to the first option instead of breaking. I would like this to only work if the user inputs a W, E, R or B. What would my best option be?
Thanks so much.
You may want to take a look at the CHOICE command instead.
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List: /C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE: 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. If the user
presses a key that is not a valid choice, the tool sounds a warning
beep. If tool detects an error condition, it returns an ERRORLEVEL
value of 255. If the user presses CTRL+BREAK or CTRL+C, the tool
returns an ERRORLEVEL value of 0. When you use ERRORLEVEL parameters
in a batch program, list them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
Adding a goto end right after the for statement. Then make a label called end after all the other labels should solve your problem. Basically the first encountered label is being executed since it failed the for statement.
So I'm making a adventure game in .bat, and I want it so that when the player presses - it takes him to the Map frame and when he presses = the player goes to the Journal. I already worked out a way to return to the frame that the player was originally in, but I need to know how to set up key bindings.
Key bindings are a key that you press at anytime, and when you press it something happens, like you go to a map or teleport.
As stated in the comments your best bet is to use choice. The main setup for choice is:
#choice [/c:] [/n] [/t /d ] [/m <"Text">]
The /c attribute lists all the available choices (i.e /c:123) would allow 1, 2 or 3 to be pressed.
The /n specifies not to display the inputted choices. (i.e the choices "123" would show up in console as [123]?) For [123]? to be shown leave out the /n
/t sets the time before a default choice is made
/d sets the default choice to be made after /t seconds
/m sets text to be displayed in the console window
For Example:
#ECHO off
#CHOICE /c:123 /n /t 100 /d 1 /m "TextGoesHere"
if ERRORLEVEL 3 GOTO three
if ERRORLEVEL 2 GOTO two
if ERRORLEVEL 1 GOTO one
goto end
:one
echo You pressed 1!
echo This is also the default choice after 100 seconds
goto end
:two
echo You pressed 2!
goto end
:three
echo You pressed 3!
:end
pause
Would display:
TextGoesHere [1,2,3]?
And if 2 was pressed:
TextGoesHere [1,2,3]?
You have pressed "2"!
press any key to continue...
And of course you could add code to these sections instead of just displaying text.
For more info about "choice" click here
I am writing a batch script, in which a user reads a disclaimer, then they press any key to continue, or "E" to exit. It looks something like this:
#echo off
echo (some disclaimer text here)
echo.
echo once you fully understand this message, press any key to continue, or press "E" to exit.
::I know that pause doesn't take input, I'm just using it as a placeholder for something
pause>nul
cls
echo Welcome!
pause
Is there any way to accomplish this? Thanks!
To clarify, I want to exit the second they press the E key. I have seen it done before, but I forgot where it saw it.
Choice.exe does what you describe, though it doesn't have an option for "any key". You can specify a specific key such as "Y" below.
echo once you fully understand this message, press "Y" to continue, or press "E" to exit.
choice /c ey
if errorlevel 2 goto :welcome
if errorlevel 1 goto :canceled
rem User pressed Ctrl+C
:canceled
rem User pressed E. Do cancel stuff.
goto :eof
:welcome
rem User pressed Y. Do welcome stuff
echo Welcome!
Is it possible in MS-DOS batch file to pause the script and wait for user to hit enter key?
I wish to do this inside a for loop. After each iteration, I want the script to pause and wait for user to hit 'Enter'
There's a pause command that does just that, though it's not specifically the enter key.
If you really want to wait for only the enter key, you can use the set command to ask for user input with a dummy variable, something like:
set /p DUMMY=Hit ENTER to continue...
You can do it with the pause command, example:
dir
pause
echo Now about to end...
pause
pause command is what you looking for.
If you looking ONLY the case when enter is hit you can abuse the runas command:
runas /user:# "" >nul 2>&1
the screen will be frozen until enter is hit.What I like more than set/p= is that if you press other buttons than enter they will be not displayed.
Depending on which OS you're using, if you are flexible, then CHOICE can be used to wait on almost any key EXCEPT enter
If you are really referring to what Microsoft insists on calling "Command Prompt" which is simply an MS-DOS emulator, then perhaps TIMEOUT may suit your purpose (timeout /t -1 waits on any key, not just ENTER) and of course CHOICE is available again in recent WIN editions.
And a warning on SET /P - whereas set /p DUMMY=Hit ENTER to continue... will work,
set "dummy="
set /p DUMMY=Hit ENTER to continue...
if defined dummy (echo not just ENTER was pressed) else (echo just ENTER was pressed)
will detect whether just ENTER or something else, ending in ENTER was keyed in.
The only valid answer would be the pause command.
Though this does not wait specifically for the 'ENTER' key, it waits for any key that is pressed.
And just in case you want it convenient for the user, pause is the best option.
npocmaka's answer made me aware of runas /user:# "" >nul 2>&1 – however, I've discovered that it can be shortened significantly, thanks to an undocumented parameter alias. 2>&1 also appears to be unnecessary. This is as short as you can make it (not counting the prompt text):
echo|set/p="Press <ENTER> to continue.."&runas/u: "">NUL
It's not very readable with syntax highlighting (and stackoverflow.com still hasn't fixed their batch file syntax highlighting), so here's a screenshot with syntax highlighting: