Saturday, June 23, 2007

"Sleep"ing in Win XP (batch files)

All examples using choice(.com) which would be among the first hits on a search don't work because Windows XP doesn't come with choice.com by default. Solutions are to get it from Win9x, a (WinNT/2003Server) resource kit, or off the MS site; or to use QBasic or a ping trick such as: ping 127.0.0.1 -n %1% -w 1000> nul

But the best solution I could find that works straight out of the box is:

@echo off
echo Starting!
echo Wscript.Sleep 10000> sleep.vbs
start /w wscript.exe sleep.vbs
echo Done!
del sleep.vbs
Off:
http://www.ericphelps.com/batch/samples/sleep.txt

As mentioned there:
1) Millisecond accuracy.
2) No 100% utilization of CPU i.e. no for(i = 1 to 1 billion) loops.

1 comment:

ksp said...

Umm .. does your file require the .bat extension? 'Cos you can create the following code:

Print "Starting!"
WScript.Sleep(10000)

And save it as sleep.vbs, and run it directly.

If you want the sleep functionality in .BAT, then this still helps.

BTW, you should look at Windows Scripting Host in more detail if you're going to do serious Windows' based scripting. Primarily, you get any-language support (VBS, JS, Ruby, Perl!)