Bat file for calling A.bat if time is less than 19:45 and for calling B.bat if time is more than 19:45

Bat file to call A.bat if time is less than 19:45, and to call B.bat if time is more than 19:45 (I cannot use the Windows task scheduler in this case, because I have a setting that forces my download manager run this parent bat file every time the file is downloaded through this download manager)

+6
batch-file
source share
4 answers

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 from the time command, and this may vary by language. The format I get is 20:17:28.48 around 8:15 pm, but your result may be different.

If so, just adjust the substrings when setting hh and mm . Team:

 set mm=!tm:~3,2! 

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


If you are not a big fan of spaghetti code, even in batch mode, you can 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 
+9
source share

Pay attention to the DATE and TIME commands.

+2
source share

I recently did something similar, and my solution for the loop cycle was not surprisingly compact, but it did its job:

 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 you just need to execute the IF statement to check if% hr% was less than 19 and% mn% was less than 45.

+2
source share

How to use windows task manager?

+1
source share

All Articles