MS-DOS Batch file pause with enter key
Asked Answered
G

7

199

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'

Gearard answered 30/4, 2013 at 14:28 Comment(0)
C
314

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...

Crin answered 30/4, 2013 at 14:34 Comment(4)
You don't need to specify a variable name: set /p=Hit ENTER to continue..., or simply set /p= if you don't need a prompt.Wenona
For some reason, my app is blasting right past the pause logic. Very strange.Keiko
It seems a bit weird to see the word "app" being used in this context (a batch file)Schild
it does not work for me. works only from second launch when 'set' worked out / saved into the env.Blackwell
D
27

You can do it with the pause command, example:

dir
pause
echo Now about to end...
pause
Dorothy answered 1/4, 2020 at 21:4 Comment(1)
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
S
16

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.

Species answered 30/4, 2013 at 14:33 Comment(0)
H
4

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.

Hopehopeful answered 30/4, 2013 at 19:33 Comment(0)
F
3

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.

Feu answered 10/9, 2020 at 12:22 Comment(0)
V
0

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:

Press  to continue..

Vivacity answered 29/12, 2021 at 18:38 Comment(0)
S
0

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.

Saliva answered 5/7, 2023 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.