The NUL device is useful for supressing "Press any key to continue"


Long time scripters will, I’m sure, know this. Useful to mention as I was writing a script over the long weekend (it was memorial day here in the US yesterday – makes up for the many UK bank holidays I’ve missed over the past couple of months!) which needed a “pause” in it, but as this was for a non-tech savvy user, I wanted to indicate that they could also press Control-C to terminate the script there and then.

If you have test.cmd with the following contents:

@echo off
echo Example 1
echo Press any key to continue or Control-C to terminate script . . .
pause

the output won’t be quite what you may want:

C:>test
Example 1
Press any key to continue or Control-C to terminate script . . .
Press any key to continue . . .

To resolve this, simply add a > NUL onto the second pause:

@echo off
echo Example 2
echo Press any key to continue or Control-C to terminate script . . .
pause > NUL

to give the following output.

C:>test
Example 2
Press any key to continue or Control-C to terminate script . . .

Cheers,
John.


Comments (3)

  1. Geoff Nordli says:

    "choice" is another good option for pausing and controlling scripts.

  2. Hi Geoff – good point, but (IIRC) I think choice is a resource kit utility from back in NT4 & 2000 days – ie not in the box by default? I’ll have to go dig it out now but thanks for the reminder.

    Cheers,

    John.

  3. Geoff Nordli says:

    It is in the default install on WS03.

    Another nice thing about choice is you can pause for X number of seconds before the script continues.

    Have a great day!

    Geoff