196

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'

dr_rk
  • 4,395
  • 13
  • 48
  • 74

7 Answers7

311

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

abjuk
  • 3,662
  • 1
  • 13
  • 7
  • 22
    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. – dbenham May 17 '13 at 03:09
  • 3
    For some reason, my app is blasting right past the pause logic. Very strange. – ouflak Oct 09 '13 at 16:16
  • 13
    It seems a bit weird to see the word "app" being used in this context (a batch file) – Adrian Grigore Jan 23 '14 at 16:53
  • it does not work for me. works only from second launch when 'set' worked out / saved into the env. – ses May 06 '14 at 19:33
27

You can do it with the pause command, example:

dir
pause
echo Now about to end...
pause
Vic_HT
  • 411
  • 5
  • 5
  • 2
    `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) – Stephan Apr 01 '20 at 21:12
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.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
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.

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

Vopel
  • 662
  • 6
  • 11
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.

ggt
  • 1