0

I tried to write a simulating a sleep with a ping request.

Here's the code:

echo STARTED-^>  %DATE% -  %TIME%>> c:\Italprogetti\logfile.log
ping 127.0.0.1 -n 3 > nul
for /L %%x IN (1,1,10) do (
    echo EXECUTED-^>%%x  %DATE% -  %TIME% >> c:\Italprogetti\logfile.log
    ping 127.0.0.1 -n 3 > nul
)
echo ENDED-^>  %DATE% -  %TIME%>> c:\Italprogetti\logfile.log

The question is: Why %TIME% remains the same for all the cycle for duration?

Here's my output:

STARTED->  21/05/2019 -  11:17:25,36
EXECUTED->1  21/05/2019 -  11:17:27,38 
EXECUTED->2  21/05/2019 -  11:17:27,38 
EXECUTED->3  21/05/2019 -  11:17:27,38 
EXECUTED->4  21/05/2019 -  11:17:27,38 
EXECUTED->5  21/05/2019 -  11:17:27,38 
EXECUTED->6  21/05/2019 -  11:17:27,38 
EXECUTED->7  21/05/2019 -  11:17:27,38 
EXECUTED->8  21/05/2019 -  11:17:27,38 
EXECUTED->9  21/05/2019 -  11:17:27,38 
EXECUTED->10  21/05/2019 -  11:17:27,38 
ENDED->  21/05/2019 -  11:17:47,58

Thanks, Federico

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    Use delayed expansion, `SetLocal EnableDelayedExpansion`, then use `!DATE! - !TIME!` within your `For` loop. – Compo May 21 '19 at 09:30
  • 1
    Please also note that there is no need to simulate a sleep using `ping.exe`, `timeout.exe` is especially for this task. Open a Command Prompt window and enter`timeout /?` to read its usage information, e.g. `Timeout /T 2 /NoBreak > NUL`. – Compo May 21 '19 at 09:42
  • 1
    @Compo, `ping` has got some advantages over `timeout`: 1. `ping` really waits for the specified time (`-n 4` waits for 3 seconds, so always decrement the given number), where `timeout` may wait shorter (`/T 3` waits for 2 to 3 seconds, because it actually counts ticks of seconds); 2. `ping` can be used together with input redirection, while `timeout` cannot... – aschipfl May 21 '19 at 10:09
  • 1
    @aschipfl, I'm aware of the uses of each, my comment was simply to inform Federico, that **`ping.exe` is not needed to simulate a sleep**. In the provided code, input redirection is not being used and exact timing certainly holds less importance than the accurate logging of the times. – Compo May 21 '19 at 10:25
  • Thank you both! you were so exhaustive. I understood that in my case there's no need to use ping, but it was the only way i known before. – Federico Pinciaroli May 21 '19 at 13:07

0 Answers0