Batch File: Time Delay

Hi there,

I did this time delay script for fun.

I'd like to make it work and also, by considering the days.

For example, if it is currently 23h on 2011-03-31, and I set 2h 05 min delay , it will expire at 1h05 on 2011-04-01.

Also, that 24h would be 0h.

I'd like to have it all as a batch file.

Thanks for your help,
Rene


@ECHO OFF

SETLOCAL enabledelayedexpansion

REM THIS IS ASSUMING THAT THE COUNT DOWN WILL FINNISH THE SAME DAY
REM W=WAIT T=TARGET EX.: WHour THour

SET WHour=0
SET WMin=1
SET WSec=10

REM SETTING UP TARGET TIME
FOR /F "tokens=1-3 delims=:, " %%A IN ("%TIME%") DO (
	SET /a THour+=%%A + %WHour%
	SET /a TMin+=%%B + %WMin%
	SET /a TSec+=%%C + %WSec%

	IF !TSec! GTR 60 (
		SET /a TMin+=1
		SET /A TSec-=60
	)
	
	IF !TMin! GTR 60 (
		SET /a THour+=1
		SET /A TMin-=60
	)
	
	IF !THour! GTR 23 (
		ECHO ERROR
		PAUSE
		EXIT
	)
)

REM WAITING FOR THE TARGET TIME TO COME
:HOME
CHOICE /D Y /T 1 >NUL
ECHO %time% | Findstr -i "!THour!:!TMin!:!TSec!"
IF !errorlevel! NEQ 1 GOTO Home

ECHO.
Echo Done
ECHO.
PAUSE

Open in new window

LVL 10
ReneGeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Rene,

While an interesting question / exercise, I think it would be a rather cumbersome task in a BAT script.  It could be done, but you would essentially have to create BAT versions of things like DateAdd() and DateDiff() from VBS.  I would personally think a better approach would be to call a small utility VBS script from the BAT script that would do the delay and then return control to the BAT file at the proper time.  What would take probably many 10's to 100's or lines of BAT code could be done in a small handfull of VBS lines.  But since you requested a BAT only solution I would prototype such a VBS here.

In addition, given the fairly robust Task Scheduler now available in Windows, I think the right way to start BAT script components at a particular date or time are better handled by that.  You can even do this dynamically at run time in the first BAT file.  It could create a new scheduled task for a second BAT file and set the start date and time of it.  Then at that time the second BAT file can continue the process.  Feels like a cleaner approach.

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ReneGeAuthor Commented:
Hmmmm, I guess the fun part of the script died with it's complexity. I don't actually need it, so for the sake of doing something creative our of this post, let's go for the VBScript call from a batch file.

Thanks bp,
Rene
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Here's a quick VBS snippet:
Dim targetTime
targetTime = DateAdd("n", 125, Now) ' 2h 5m minutes from now

' check every 5 seconds to see if our targetTime has been passed
While targetTime > Now
    WScript.Sleep 5000 ' (max is 32,767 --> approx 30 seconds)
Wend

' ... we reach this point after the target time has been reached ...

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ReneGeAuthor Commented:
Hey bp and Idle_Mind,

I almost got it as a batch file.

Will let you know.

Cheers,
Rene

PS: Thanks Idle_Mind for the VBS snippet
0
Bill PrewIT / Software Engineering ConsultantCommented:
Look forward to seeing that Rene.

~bp
0
aavictorCommented:
Simple Solution:
Download powershell from MS, and there is a command "sleep"
Use this if you like.
Or install cygwin from cygwin.com.  this shell has also a command "sleep"

sleep 10 for 10 second delay
0
ReneGeAuthor Commented:
Thanks aavictor

But I don't need a sleep command. I want to augment my batch file, for fun, so it cat do this.

I will comlete it soon ans let you know.

Cheers,
Rene
0
ReneGeAuthor Commented:
Oh my my... I really messed up my previous post.  Let's try this again.

Thanks aavictor

But I don't need a sleep command. I want to augment my batch file, for fun, so it can do a sleep command, the hard way.

I will complete it soon and let you know.

Cheers,
Rene
0
ReneGeAuthor Commented:
It needs some fine tuning but here is I have so far.

I'll continue working on it tonight.

If you feel like contributing, go ahaid.

Cheers,
Rene

 
@ECHO OFF


REM S=START W=WAIT T=TARGET EX.: WHour THour

SET WDay=0
SET WHour=0
SET WMin=0
SET WSec=5

CALL :GetDateTime
SET SYear=%YearVal%
SET SMonth=%MonthVal%
SET SDay=%DayVal%
SET SHour=%HourVal%
SET SMin=%MinVal%
SET SSec=%SecVal%

REM SETTING UP TARGET DATE AND TIME
	SET /a TDay+=%JDate% + %WDay%
	SET /a THour=%SHour% + %WHour%
	SET /a TMin=%SMin% + %WMin%
	SET /a TSec=%SSec% + %WSec%

	IF %TSec% GEQ 60 (
		SET /a TMin+=1
		SET /a TSec-=60
	)
	
	IF %TMin% GEQ 60 (
		SET /a THour+=1
		SET /a TMin-=60
	)
	
	IF %THour% GEQ 24 (
		SET /a TDay+=1
		SET /a THour-=24
	)

REM CONVERT TDay TO GDay
	CALL :ConvertToGdate %TDay%


REM WAITING FOR THE TARGET TIME TO COME
:HOME
CHOICE /D Y /T 1 >NUL
TITLE START=%SYear%-%SMonth%-%SDay% %SHour%h %SMin%m %SSec%s  TARGET=%GYear%-%GMonth%-%GDay% %GHour%h %GMin%m %GSec%s
CALL :GetDateTime

ECHO START:[%YearVal%.%MonthVal%.%DayVal%.%HourVal%.%MinVal%.%SecVal%] 
ECHO TARGT:[%GYear%.%GMonth%.%GDay%.%THour%.%TMin%.%TSec%]
ECHO.
PAUSE
EXIT



:GetDateTime
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	IF %%A GTR 0 (
	SET DayVal=%%A
	SET HourVal=%%B
	SET MinVal=%%C
	SET MonthVal=%%D
	SET SecVal=%%E
	SET YearVal=%%F
	)
)

REM CONVERT DATE TO JULIAN
	SET /a "YearJ=10000%YearVal% %%10000,MonthJ=100%MonthVal% %% 100,DayJ=100%DayVal% %% 100"
	SET /a JDate=DayJ-32075+1461*(YearJ+4800+(MonthJ-14)/12)/4+367*(MonthJ-2-(MonthJ-14)/12*12)/12-3*((YearJ+4900+(MonthJ-14)/12)/100)/4
	
EXIT /b


:ConvertToGdate

SET /A P      = %1 + 68569
SET /A Q      = 4 * %P% / 146097
SET /A R      = %P% - ( 146097 * %Q% +3 ) / 4
SET /A S      = 4000 * ( %R% + 1 ) / 1461001
SET /A T      = %R% - 1461 * %S% / 4 + 31
SET /A U      = 80 * %T% / 2447
SET /A V      = %U% / 11
SET /A GYear  = 100 * ( %Q% - 49 ) + %S% + %V%
SET /A GMonth = %U% + 2 - 12 * %V%
SET /A GDay   = %T% - 2447 * %U% / 80
:: Clean up the mess
FOR %%A IN (P Q R S T U V) DO SET %%A=
:: Add leading zeroes
REM IF 1%GMonth% LSS 20 SET GMonth=0%GMonth%
REM IF 1%GDay%   LSS 20 SET GDay=0%GDay%
:: Return value
SET GDate=%GYear%-%GMonth%-%GDay%

EXIT /b

Open in new window

0
ReneGeAuthor Commented:
I'm too busy to further work on this for now, so I'll split the points and update it when I can.

Thanks a bunch and cheers,
Rene
0
Bill PrewIT / Software Engineering ConsultantCommented:
Fair enough Rene, I have a similar task in my creative backlog...

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.