Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

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
Avatar of Bill Prew
Bill Prew

Look forward to seeing that Rene.

~bp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

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
Avatar of ReneGe

ASKER

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
Avatar of ReneGe

ASKER

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

Avatar of ReneGe

ASKER

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
Fair enough Rene, I have a similar task in my creative backlog...

~bp