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'
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
indeed is the only valid answer for MS-DOS
(all other answers are working in cmd
only). So, your answer is actually the only completely correct one for this special question (although it was already suggested more than 7 years before) –
Lavern 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:
Use pause in your bat file without any parameters like so:
@echo off
echo Hello
pause
Running this by double-clicking it in Windows Explorer will display:
Hello
Press any key to continue . . .
screenshot of cmd running the code
After pressing any key (excluding modifier keys), the code will continue and if it ends, the window will disappear.
@echo off
prevents the commands from being printed.© 2022 - 2024 — McMap. All rights reserved.
set /p=Hit ENTER to continue...
, or simplyset /p=
if you don't need a prompt. – Wenona