1

I have a problem on my computer. From time to time, certain features stop working, for perhaps 1-5 minutes. Ping ceases to work when this occurs. So I thought I'd gather some stats about this issue via a batch file that would run ping perhaps 1000 times and echo the time when it started and then again when it stopped. This is the file:

echo off
set i=
FOR /L %%i IN (1,1,3) DO (

   echo %time%
   ping www.google.com
   echo %time%
)

But what happens is the first time echoed is the same as the second time. WHy? Or is there a better way to do this?

Ron
  • 2,435
  • 3
  • 25
  • 34

1 Answers1

0

inside a command block (surrounded by (parantheses)), you need delayed expansion:

@echo off
setlocal enabledelayedexpansion
FOR /L %%i IN (1,1,3) DO (
   echo !time!
   ping www.google.com
   echo !time!
)
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • You don't need delayed expansion inside all commandblocks, for example redirect command blocks don't need it. Only if and for as far as I am aware. – Dennis van Gils Jan 27 '16 at 15:46
  • It seems you're right, I really thought only the for and if need it. Thanks for letting me know. – Dennis van Gils Jan 27 '16 at 16:27
  • @DennisvanGils Syntax: `if a==a `, where `command` might also be a (block of commands). Doesn't matter, if there is a IF statement in front of it or not. (same with `for .... do `) – Stephan Jan 27 '16 at 16:33