?
Solved

Defining time in batch

Posted on 2016-07-28
7
Medium Priority
?
66 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 56

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 56

Accepted Solution

by:
Bill Prew earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 56

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 56

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 56

Expert Comment

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

~bp
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 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