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.
Related
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.
Please help, I'm a beginner and I need to create batch-file to delete log-files. But before delete I need to ask user to delete the last log-file or all log-files.
I tried to implement it like:
set /P PARAM_VALUE=Remove only the last log file?(Y/N)?
if /I "%PARAM_VALUE%"=="y" goto DeleteLast
if /I "%PARAM_VALUE%"=="n" goto DeleteAll
But in this case I can enter any values, I need to avoid an others character except Y,y,N,n.
Thx.
One of solutionі in this case is using the CHOICE command.
This tool allows users to select one item from a list of choices and returns the index of the selected choice.
Syntax: CHOICE [ /C choices ] [ /N ] [ /CS ] [ /T timeout /D choice ] [ /M text ]
Parameter List:
/C choices Specifies the list of choices to be created. Default list for English versions 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. Note: DOS and NT Resource Kit versions use /S instead
/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.
Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
/D default Specifies the default choice after timeout seconds.
Character must be in the set of choices specified by /C option and must also specify timeout with /T.
Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
/M text Specifies the message to be displayed before the prompt.
If not specified, the utility displays only a prompt.
For your problem try to use something like:
choice /C:YN /M:"Remove only the last log file? [YN]"
IF ERRORLEVEL ==1 GOTO DeleteLast
IF ERRORLEVEL ==2 GOTO DeleteAll
GOTO next
:DeleteAll
rem delete all
:DeleteLast
rem delete last
:next
rem continue work with Batch file
You can just catch all other inputs like this:
:CONFIRM
if /i "%PARAMVALUE%"=="y" goto DeleteLast
if /i "%PARAMVALUE%"=="n" goto DeleteAll
echo Please enter only Y/y or N/n!
ping localhost -n 2 >nul
goto CONFIRM
I used this too for my own scripts and it works wonderfully and is super easy!
The delete command already offers a prompt N/y
EDIT
del /p
type del /? in console for more information
UPDATE
you can test with this example:
#echo off
SETLOCAL
Echo:
Echo /!\ Creating a bunch of empty files /!\
echo Press any key to continue or CTRL-C to exit...
Pause>nul
for /L %%A in (1,1,4) do (copy /y NUL _temp_file_%%A.ext)
Echo:
Echo /!\ Deleting a bunch of empty files with command prompt /!\
echo Press any key to continue or CTRL-C to exit...
Pause>nul
del /p "_temp_file_?.ext"
Echo:
Pause>nul|(echo Press any key to exit...)
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
Ok, so you know how when you type up pause in CMD, it will say 'Press any key to continue...'. How do I change that to say something like 'Press a key to proceed...'?
Lastly, I was coding a batch file. I want to know what's up if I have something like:
#echo off
cls
pause
pause
pause
pause
It seems to skip round about to pauses When you press a key. I'm curious as to know the rules of which the pauses are skipped.
Thanks.
Nearly the same as Deniz Zoeteman, except this version displays the blinking cursor on the same line as your custom message, as does the normal PAUSE command. The Deniz Zoeteman solution displays the blinking cursor below your message.
<nul set /p "=Press a key to proceed..."
pause >nul
You cannot change the text displayed when a pause command is executed. It's bound to the Windows installation's language pack. The only thing you can do is not letting it say anything by doing pause>nul.
Of course, there's different ways to simulate pause; see the example from the other answer, where set /p is used. With pause>nul however, you can do this:
echo Custom pause message
pause>nul
And that should work.
And for pause commands skipping, that's most likely due to the key still being pressed down while the next pause command already executed (small guess though - I don't recall exactly if that's the behaviour of the command).
You may want to try
set /p=your message
Note: you will have to hit the enter key to continue versus any key.
Option #1:
#echo off
echo Press any key to continue or Ctrl-C to abort.
pause > nul
Option #2
set /p=Press any key to continue or Ctrl-C to abort.
-->> I prefer the screen output of option #1.
This is one practical application:
set /p=Press ENTER key to proceed or Ctrl-C to abort.
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: