dhiraj05
dhiraj05

Reputation: 61

Bat file to call A.bat if time is less than 19:45 and to call B.bat if time is greater than 19:45

Bat file to call A.bat if time is less than 19:45 and to call B.bat if time is greater than 19:45 (i can not use windows task scheduler in this case because i have setting which makes my download manager to trigger this parent bat file each time a file is downloaded through this download manager)

Upvotes: 6

Views: 13308

Answers (4)

user2483876
user2483876

Reputation: 139

I was recently doing something similar and my solution was a for loop - not amazingly compact but got the job done:

for /f "tokens=1,2,3,4 delims=:,. " %%i in ("%time%") do (

echo Timegrab = %%i %%j %%k %%l
set hr=%%i
set mn=%%j
set sc=%%k
set ms=%%l

)

Then all you would need to do is an IF statement to check whether %hr% was less than 19 and %mn% was less than 45.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881383

You can use the following code as a baseline (you can use bat files but I prefer cmd as an extension):

@echo off
setlocal enableextensions enabledelayedexpansion
set tm=%time%
:: Test data on lines below.
:: set tm=18:59:59.00
:: set tm=19:00:00.00
:: set tm=19:44:59.00
:: set tm=19:45:00.00
:: set tm=23:59:59.99
set hh=!tm:~0,2!
set mm=!tm:~3,2!
if !hh! lss 19 (
    call a.cmd
    goto :done
)
if !hh! equ 19 (
    if !mm! lss 45 (
        call a.cmd
        goto :done
    )
)
call b.cmd
:done
endlocal

Keep in mind that %time% is the same format as you get from the time command and this may depend on locale. The format I'm getting is 20:17:28.48 for around 8:15pm but your result may be different.

If it is, just adjust the substrings when setting hh and mm. The command:

set mm=!tm:~3,2!

sets mm to the two characters of tm at offset 3 (where offset 0 is the first character).


If you're not a big fan of spaghetti code, even in batch, you could also use:

@echo off
setlocal enableextensions enabledelayedexpansion
set tm=%time%
:: Test data on lines below.
:: set tm=18:59:59.00
:: set tm=19:00:00.00
:: set tm=19:44:59.00
:: set tm=19:45:00.00
:: set tm=23:59:59.99
set hh=!tm:~0,2!
set mm=!tm:~3,2!
if !hh! lss 19 (
    call a.cmd
) else (
    if !hh! equ 19 if !mm! lss 45 (
        call a.cmd
    ) else (
        call b.cmd
    )
)
endlocal

Upvotes: 10

JRL
JRL

Reputation: 77995

Check out the DATE and TIME commands here.

Upvotes: 2

AndrewC
AndrewC

Reputation: 6730

How about using Windows Task Scheduler?

Upvotes: 1

Related Questions