Link to home
Start Free TrialLog in
Avatar of ivan rosa
ivan rosaFlag for United States of America

asked on

Defining time in batch

Hello folks,

I have this endless loop below, but I'm trying to get it running for let's say 1 minute duration, how would I set the condition for time?


@echo off
cls
:start
 echo "starting loop"
 ping www.sitetotest.com >>log.txt
goto start

Open in new window

Avatar of Bill Prew
Bill Prew

There isn't a clean easy way to do this in BAT script unfortunately, it's ability to do date and/or time calculations is very limited.  That being said, here is an earlier question with a discussion and hybrid solution:

https://www.experts-exchange.com/questions/27840118/Generate-Time-Elapsed-in-Batch-file.html

If you wanted to work in Powershell or VBS instead they can readily do these types of calculations, but not sure that works for you.

Would it be okay for you to leverage a "stopwatch" type add on EXE from your BAT script.  There are several of these (I mention one in that link above) that you could potentially use to calculate the elapsed time and wait until it exceeded your desired interval, like 1 minute.  Would that be feasible?

~bp
Avatar of ivan rosa

ASKER

please a  ps1 or a vbs are always welcome
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is an example of a Powershell approach that you could adapt.

https://mjolinor.wordpress.com/2012/01/14/making-a-timed-loop-in-powershell/

~bp
Here's a simple example of how to do it in VBS:

Wscript.Echo Now

EndTime = Timer + 10 ' seconds
Do 
   ' run ping command here
   Wscript.Sleep 1000 ' millisecoinds
Loop While Timer < EndTime

Wscript.Echo Now

Open in new window

~bp
super! bill you are a beast!
Welcome, glad that was helpful, thanks for the feedback.

~bp