1

I have searched intensively but have only found wait-tips in batch files in seconds units (the ping approach i.e.).

Since I have a need to wait in between two commands only for something like 10-100 milliseconds, that does not work for me unfortunately.

The wait-time needs not to be "super accurate". In my case, does not make a big difference if it's 10ms or 12/15ms, but it should be able to distinguish between 10 and, say 20.

I wonder if there is a solution, and if in any way possible, using just the windows "on board" commands / tricks as I want to have just the batch for ease of "installation" when using later on another machine.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jwka
  • 115
  • 7
  • I don't think, its possible with a resolution less than 100ms. Try: `@echo off :loop echo %time% goto loop` (sorry, no linefeed in comments, but i think, you know, where to set them). Do you see that even the second digit (10ms) after the comma does not count consecutive? (btw: about 60 to 70 of those echos gives you "about" 100ms - which does NOT mean, 6 to 7 will give about 10ms...) – Stephan Sep 26 '13 at 10:05

3 Answers3

3
@echo off

echo Time before: %time%
echo Wait %1 centiseconds (or more)
call :wait %1
echo Time after:  %time%
goto :EOF

:wait centiseconds
for /F "tokens=3,4 delims=:." %%a in ("%time%") do set /A sec=1%%a, msec=1%%b+%1
if %msec% gtr 199 set /A sec+=1, msec-=100
set lim=%sec%%msec%
:waitHere
for /F "tokens=3,4 delims=:." %%a in ("%time%") do if 1%%a1%%b lss %lim% goto waitHere
exit /B

The minimum precise wait time entirely depends on the speed of the computer. For example, in my case a wait time less than 5 centiseconds always waits for more time. If I use a wait time from 5 centiseconds on, the timing is precise.

EDIT: I slightly modified the program in order to make it more precise with small wait times, it is now precise in my computer from 3 centiseconds on!

You must note that the minimum precise wait time also depends on the size of the Batch file; if the file is large, the minimum precise time increases...

Aacini
  • 65,180
  • 12
  • 72
  • 108
0

You can use the %time% variable and calculate the difference.
In pseudo code it looks like this.

set startTime=%time%

:wait
set now=%time%
if now-startTime < waitTime goto wait

Btw externally programs will not work here, as the loading/starting time is not predictable and can be much longer as your waittime.

jeb
  • 78,592
  • 17
  • 171
  • 225
0

this might do, what you want:

@echo off
echo %time%
for /L %%i in (1,1,50) do ( echo %time% )>nul
echo %time%

for example three consecutive runs:

C:\>t
12:15:20,74
12:15:20,75

C:\>t
12:15:21,22
12:15:21,22

C:\>t
12:15:21,69
12:15:21,71

As you see, the first run gives you "about" 10ms, the second run gives you "about" 0ms, the third one gives you "about" 20ms. Nothing reliable...

btw: if you replace echo %time% with echo time it's getting worse...

Stephan
  • 53,940
  • 10
  • 58
  • 91