Solved

Defining time in batch

Posted on 2016-07-28
7
48 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
  • 5
  • 2
7 Comments
 
LVL 51

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 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

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 51

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 51

Expert Comment

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

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now