Here’s a quick tip that I recently ran across. Maybe it’s old news but I haven’t seen it before.

Sometimes you want to pause or sleep a few seconds in a command/batch script. By default Windows doesn’t have any form of a “sleep” command installed that you can use in script files. Sure there’s versions of sleep.exe or similar programs that you can install but there’s another utility that’s already installed by default that you can use*. It’s called “choice” and it’s normally used to prompt a user to make a choice between several options. The trick is, it has a default choice timeout that can be set in seconds.

So here’s how to use choice.exe to pause or sleep for 10 seconds in a command script or batch file:

choice /T:10 /D N /N > Nul

Here we’re telling choice.exe to select the default choice, set by /D, in 10 seconds as set by the /T option. The /N and redirection to Nul just keeps the console output clean. I’ve tried with with fairly large timeouts and it seems to work for me. The upper limit appears to be 9999 seconds, which is roughly 2.75 hours.

btw, another common way that I’ve seen used to pause or sleep in a batch file is to use “ping” to repeatedly ping an IP with a timeout value but this method seems much cleaner to me.

* Note: Unfortunately, choice.exe isn’t available by default on Windows XP but it is on Windows Server 2003, Vista, and presumably all later versions of Windows.

Comments are closed

Flux and Mutability

The mutable notebook of David Jade