C:\_G\WWW\~ELISANET\INFO\tscmd019.html
<https://www.salminet.fi/info/tscmd019.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 30-Nov-2018 10:51:17

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



19} How can one build a delay / sleep / wait procedure for a script?

The most often presented delay trick is using ping. For a two second delay one can ping oneself as follows
  @echo off
  echo %time%
  ping -n 3 127.0.0.1>nul
  echo %time%

which would produce e.g.
  D:\TEST>cmdfaq
  15:24:47.57
  15:24:49.62
As you see, it is not dead accurate. But neither is the solution below solution. Not that it usually is essential.

This could, of course, be written as a subroutine in case the delay needs to be called several times during the script.
  @echo off & setlocal enableextensions
  echo %time%
  call :SmallDelay
  echo %time%
  call :SmallDelay
  echo %time%
  endlocal & goto :EOF
  ::
  :SmallDelay
  ping -n 3 127.0.0.1>nul
  goto :EOF
The output could be e.g.
  D:\TEST>cmdfaq
  11:36:27.98
  11:36:30.03
  11:36:32.09

Another method is to observe the difference between the time the delay is started and the current time. The calculations are in hundreds of a second since midnight. (The rare potential case of straddling the midnight is not covered.) The example below sleeps for about two and a half seconds.

  @echo off & setlocal enableextensions
  echo %time%
  call :ProcDelay 250
  echo %time%
  endlocal & goto :EOF
  ::
  :ProcDelay delayMSec_
  setlocal enableextensions
    set time_=%time%
    set time_=%time_::0=:%
    set time_=%time_:.0=.%
    set time_=%time_: =%
    for /f "tokens=1-4 delims=:. " %%h in ('echo %time_%') do (
      set /a start_=360000*%%h+6000*%%i+100*%%j+%%k)
    :_procwaitloop
      set time_=%time%
      set time_=%time_::0=:%
      set time_=%time_:.0=.%
      set time_=%time_: =%
      for /f "tokens=1-4 delims=:. " %%h in ('echo %time_%') do (
        set /a now_=360000*%%h+6000*%%i+100*%%j+%%k)
      set /a diff_=%now_%-%start_%+3
    if %diff_% LSS %1 goto _procwaitloop
  endlocal & goto :EOF

The output e.g:
  D:\TEST>cmdfaq
  19:49:06.94
  19:49:09.45
Why the set time_=%time_::0=:% lines? To avoid the octal number catch. Octals are (confusingly) identified by a leading zero.

Furthermore, you could have a Visual Basic Script (VBScript) aided command line script
  @echo off & setlocal enableextensions
  echo WScript.Sleep 1000 > "%temp%\tmp$$$.vbs"
  echo %time%
  cscript //nologo "%temp%\tmp$$$.vbs"
  echo %time%
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  endlocal & goto :EOF

which would produce e.g.
  D:\TEST>cmdfaq
  13:13:03.00
  13:13:04.07

References/Comments:
  hh ntcmds.chm::/ping.htm [You can't use this unless you have the ntcmds.chm file]

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd019.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd019
https://www.salminet.fi/info/tscmd019.html
file:///c:/_g/www/~elisanet/info/tscmd019.html