Avatar of ivan rosa
ivan rosa
Flag 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

Windows BatchVB ScriptPowershell

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
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
ivan rosa

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Bill Prew

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
ivan rosa

ASKER
super! bill you are a beast!
Bill Prew

Welcome, glad that was helpful, thanks for the feedback.

~bp
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.