MS-DOS Batch file pause with enter key - batch-file

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:

Related

Specific Keystroke for batch

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.

Catching keystroke during loop in batch

As a small training for my batch programming I originally followed the idea to make up a small slotmachine. You can give your coins, the numbers rush through and when you hit a button, the numbers should stop at their current value. If they are all the same, boom, you won the jackpot!
My problem right now is the keystroke capture during the loop. I already thought something like a choice command, but then the program would stop at each loop waiting for keyboard input, not making the game just quite annoying to wait all the time, but as well boring as you could check if you want to hit a specific button to stop.
Another thought was putting
set /p foobar=
and then simulate an Enter-Stroke with !SendKeys! (with everything neccessary in the code), forgetting that the enter yould have been sended after input...
Is there a way to accomplish that in ONE batch file? Or do I have to come up with another one to simulate the keystrokes or is there anything else I have missed?
EDIT: To clearify: Is there any command that changes something on keystroke, but runs through if nothing is touched?
Thanks for help in advance!
Greetings
geisterfurz007
#echo off
setlocal EnableDelayedExpansion
rem Create an empty file
cd . > key.txt
rem Start a parallel process that wait for Enter key
rem and add a line to the empty file
start "" /B cmd /C "set /P = & echo line >> key.txt"
set "key="
:wait
cls
set "number=%random:~-4%"
echo %number%
echo/
echo Press Enter key to stop the numbers...
set /P key=< key.txt
if not defined key goto wait
echo The last number is: %number%

bat file programmatically press enter to go next line

When I run following command on cmd it ask me to press enter to continue. when I press enter it show the next thing and so on.we do this when develop/create a new firefox addon using jpm tool.first it show default title if I hit enter it show default name and etc...
command jpm init
here is an snapshot
now I want to make a bat file for this. so it should go to next line like when I press enter .
I tried this. but it doesn't go to next line it show title..and wait..
create.bat
call jpm init
echo
echo
echo
echo
echo
echo
pause
How can I make a bat file for this?
Try this:
(
echo/
echo/
echo/
echo/
echo/
echo/
) | call jpm init
pause
another method, that may (or may not) work for you:
<nul call jpm init
Whenever an input is required, it is taken from the NUL device.
Pro: no need to know how many inputs are required
Con: every input request is answered with RETURN

How do I test for button press on pause command in batch?

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!

Changing Pause Message

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.

Resources