Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I have been trying for several days to write a batch File which will simply start a particular progarm depending if the time of day is either before, or after 9.00am....something like this:
01 get time
02 if time less than 9.00am start program one.exe
03 if time later than 9.00am start program two.exeI am a senior cit at 71 years of age and just can't get this to work.....any assistance will be greatly appreciated. Thanks........\\

I'm sure it could be dine better, but this is the way I would do it.
@echo off
IF %time:~0,1% LSS 9 SET L=1
IF %time:~0,2% GEQ 9 SET G=1
IF %L%==1 START program for if it is before 9
IF %G%==1 START program for after 9EXIT
I only Batch if possible, 2000 more lines of code, oh well.

Thank you BatchFreak...but I have the same problem as I have been having for the past week.....
If I run your file and the time is LATER than 0900..(say 10.45am)......BOTH program one and two "start" (run)
If I adjust the time to read BEFORE 0900..(say 5.30am)...then NEITHER program will "start" (run)
Any suggestions please.........\\

I did the variable parsing wrong... I tested this one, and it works.
IF %time:~0,2% GTR 9 ( Start notepad.exe
EXIT
)
IF %time:~0,2%== 9 (
Start Notepad.exe
EXIT
)
IF %time:~0,2% LSS 9 START firefox.exeI only Batch if possible, 2000 more lines of code, oh well.

Thank you....thank you......but please forgive a silly question. But...where do I insert the program name(s)into the script?...ie,
the program to run before 9.00am (say 4.25am)is located in the same directory as this Batch File.
The program to run after 9.00am (say 2.35pm)is located in the root directory of the C Drive (C:\)

you put it where I wrote firefox.exe and notepad.exe
I only Batch if possible, 2000 more lines of code, oh well.

Back again....you must have edited a Post as I was typing an answer....but...but this last one works wonderfully. I am indebted to you.
For your information I am running a little program to monitor my download traffic. My ISP does NOT provide a "real time" solution and the data displayed is usually 24 to 36 hours old.
The programs (one) and (two) I spoke about earlier are the same program...but are located in differing folders so THEIR respective .ini files do not clash. Program one monitors my OFFPEAK traffic (between 0400 and 0900)...and Program two does the same thing between the hours of 0900 and 0400 etc etc.
The batch File you have given me causes the computer to "start" the appropriate program depending on what time I turn it on each day.
I have two (2) items running in Task Scheduler....both are activated at 0900.
The first kills the Off Peak Meter (if it has been started)...the second scheduled task starts the Peak Net Meter etc etc
For my own learning curve...is it posible to build a loop into what you have already written so that %time% is monitored so that program one can be stopped at 9.00am (if it's running) and replaced with program 2??
Thanks again...........\\

Yes... And no. If you want your batch file to run in the background constantly you can use...
****Please note that for 1 second at 9:00 and midnight your PC may seem to flash.****@echo off
:start
IF %time:~0,2% GTR 9 (
Start After 9
EXIT
)
IF %time:~0,2%== 9 (
Start AFTER 9
EXIT
)
IF %time:~0,2% LSS 9 START before 9:middle
IF %time:~0,5%== 0:00:00 (
TASKKILL.exe /f /im After 9
GOTO start
)
IF %time:~0,5%== 9:00:00 (
TASKKILL /f /im before 9
GOTO start
)
GOTO middle

Hi again,
Two points...first...where do I insert "program 1 and 2" etc.
Second.....I see you are using "TASKKILL.exe".
I am using XP (Home)...Taskkill is NOT included other than XP Professional.......it took me a long time to work this out....I use PSKILL instead...not sure if it will work with the above though???
Finally...where are you???...I am in Brisbane...east coast Australia.\\

First you insert program one and progrma two where i wrote, before 9 and after 9
I live in Canada.the syntax is different you would write it like this instead for XP HOME.
@echo off
:start
IF %time:~0,2% GTR 9 (
Start After 9
)
IF %time:~0,2%== 9 (
Start AFTER 9
)
IF %time:~0,2% LSS 9 START before 9:middle
IF %time:~0,5%== 0:00:00 (
PSKILL.exe -t After 9
GOTO start
)
IF %time:~0,5%== 9:00:00 (
PSKILL.exe -t before 9
GOTO start
)
GOTO middleI only Batch if possible, 2000 more lines of code, oh well.

Thank you Sir...all muchly appreciated...have wife's birthday party to get ready for....better turn this beast off before I start getting yelled at........I will set up latest script and test tomorrow and will get back to you then.
Cheers......\\

All working very nicely. Tks BatchFreak.
If you are ever driving past my door (wink)...drop in for a cuppa.
Cheers.....\\

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |