Solved

Defining time in batch

Posted on 2016-07-28
7
54 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 52

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 52

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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 52

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 52

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 52

Expert Comment

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

~bp
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sccm client without collection 1 41
PowerShell select-object returns empty lines 3 17
xcopy loop 13 45
Identify disabled AD users with PowerShell 6 39
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A procedure for exporting installed hotfix details of remote computers using powershell
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

18 Experts available now in Live!

Get 1:1 Help Now