[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Defining time in batch

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

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 58

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

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 58

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 58

Expert Comment

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

~bp
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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