views:

746

answers:

5

I came past a few ways to cause a time delay such as pings and dirs. Though none of them are really precise, is there anny proper way to cause a time delay?

I heard about a few things though they don't work on all computers, not on my Windows XP nor the Windows NT at college.

It takes ages going through all files on Google finding a good answer, and since I didn't yet find the question on Stack Overflow I thought it might be good to just create the question myself ;)

+6  A: 

Sleep

It will allow you to do this.

aintnoprophet
I see, ill try it out tough it's not realy ideal since the .bat is made to be compatible with anny pc, so i'd have to download that file evry time...but ill look what i can do with it, ty for your reaction :)
billyy
I see, i was able to find a great way to use this. So its probably the best way to do it. Slightly adjusted but yeah, this does it.
billyy
A: 

"...proper way...."

you can't do that in DOS.

Keng
people say that a lot, tough you must be able to create it, even if you just create a command that will if i like enter 5 seconds just repeat a command 5 times that keeps it busy for 1 second...
billyy
I think he meant: You can't do that in DOS without significant hackery.
Ben S
+3  A: 

<warning>This is a hack</warning>

Use your favorite programming language (other than MS-DOS batch) and create an application which takes one argument, the number of milliseconds to wait, then, simply call this program from a batch file to sleep the required amount.

As far as I know, this is the only reliable way to do it in DOS.

Ben S
It's not what i used but it is a great way to solve it, tough i didn't realy know a language that would be of anny use in this case.
billyy
+2  A: 

If you don't have the ability to send another program along with the batch file, use DEBUG to write a sleep command on the fly, and execute it.

EDIT: The above answer was kind of toungue-in-cheek. I wouldn't run a batch file that had some DEBUG trickery in it. I believe the traditional way to use a delay in a batch file is the CHOICE commad.

type nul|choice /c:y /t:y,nn > nul

Which of course, doesn't work in XP, since that would be WAAYY too convenient.

Eclipse
well i could ofcourse let it write a php script than execute it, php script hides, waits, and deletes the window after witch the batch wil go on...
billyy
A: 

It is possible to achieve a precision of a few miliseconds, depending on your machine's speed.

I have just finished creating such a batch, and though I won't share with you the actual code, I'll give you some pointers:

  1. Use %time% variable, and devide into substrings (without ":" and ".") - one substring will get the seconds, the other - minutes (you may add hours, for delays of over an hour, and even incorporate the date)

  2. Use set /A to transform the time variables into 1 integer representing the amount of seconds which have passed since a rounded hour (X:00:00.00). Add the inputed delay (in seconds) to that.

  3. Create a short loop, comparing the value of the combined var (explained in section 2) to the current time (need to recalc the curent combined min+sec value, inside this loop), and exiting the loop when the match is true.

One major bugfix here - you'll need to truncate any preceeding zeros to variables which are about to get evaluated in a "set /A" command. I've noticed that the console's evaluator (for this command) returns an error when an integer with a preceeding 08 or 09 (or simply 08 or 09) is used.

Remark: This will only work with command extensions enabled. To test it, use the following at the beginning of the routine:

verify other 2>nul
setlocal enableextensions
if errorlevel 1 goto err

And then add an error handler subroutine named "err".

If you'd want to continue your batch in the same file, use "endlocal" after the looping subroutine.

BTW, this applies ONLY to Windows XP service pack 2 or 3.