Sleep command in batch file?

jayjyli picture jayjyli · Oct 31, 2011 · Viewed 50.9k times · Source

When I'm writing a batch file to run automatically, how do I write it so that when the batch file is run, it can pause for a couple seconds in between commands?

Context:

psexec \\server -u user -p pass cmd
[there needs to be a pause here for psexec to establish a connection]
dir /s >output.txt \\server\shared

*Note: the reason I run the dir command server-side using psexec and not locally is because it's much faster to run dir on a local machine than remotely, and time is of the essence.

When I'm doing this by hand it's obviously easy, I just wait. But running a batch file makes it run all commands at near instant speeds next to each other, regardless of the completion status of the last command. How do I put in a pause?

Answer

softmllx picture softmllx · Oct 31, 2011

On Windows Vista / Windows 7 you can use the timeout command:

timeout /T [delay in seconds] /NOBREAK > NUL

On previous versions of Windows, you can use the ping command (the ping command has 1000 ms of delay between each iteration):

ping -n [delay in seconds + 1] 127.0.0.1 > NUL

Some versions of Windows (like Windows Server 2003) has the sleep.exe executable:

sleep [delay in seconds]

Note: Windows Resource kit for 2003 contains sleep.exe command.

If you don't know the Windows version, simply use the ping hack since it'll be available.