Solved

Defining time in batch

Posted on 2016-07-28
7
61 Views
Last Modified: 2016-07-28
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

0
Comment
Question by:ivan rosa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 41733622
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
0
 
LVL 3

Author Comment

by:ivan rosa
ID: 41733668
please a  ps1 or a vbs are always welcome
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41733671
If you want a simple BAT approach, with some limitations, here's an example adopted from an earlier solution I did.  It won't work if the loop crosses over midnight, since time values decrease when that happens.  Also, it assumes your %TIME% variable displays in a format like HH:MM:DD.hh, if yours doesn't we could adapt to use WMIC or some other way to get the current time.

@echo off
setlocal EnableDelayedExpansion

rem Define how many seconds to PING for
set MaxSeconds=60

rem CLear scree and save the start time of PING loop
cls
set StartTime=%TIME%

rem Keep pinging for defined time
:PingLoop
  ping www.sitetotest.com >>log.txt
  set CurrentTime=%TIME%
  call :ElapsedTime %StartTime% %CurrentTime% "ElapsedSeconds"
  if !ElapsedSeconds! LSS %MaxSeconds% goto :PingLoop

REM Continues here after 1 minute of pings...
echo Hello world.

REM End main script before subroutines
exit /b


REM Subroutine to calculate the seconds between two times
REM times in HH:MM:SS.mm format (or HH:MM:SS or HH.MM.SS)
:ElapsedTime "time-from" "time-to" "return-variable-name-elapsed-seconds"
  for /F "tokens=1-3 delims=:." %%A in ("%~1") do (set hh=%%A & set mm=%%B & set ss=%%C)
  set /A s1=ss+(mm*60)+(hh*3600)
  for /F "tokens=1-3 delims=:." %%A in ("%~2") do (set hh=%%A & set mm=%%B & set ss=%%C)
  set /A s2=ss+(mm*60)+(hh*3600)
  set /A e=s2-s1
  if %e% GTR 86400 (set /A e-=86400) else (if %e% LSS 0 set /A e+=86400)
  set %~3=%e%
  exit /b

Open in new window

~bp
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 54

Expert Comment

by:Bill Prew
ID: 41733675
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
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 41733697
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
0
 
LVL 3

Author Closing Comment

by:ivan rosa
ID: 41733822
super! bill you are a beast!
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 41733930
Welcome, glad that was helpful, thanks for the feedback.

~bp
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question