简体   繁体   中英

Run a Batch file between specific time in a day

I want to built a logic for my windows batch script that it should only run between 7:00 and 23:00 daily.

I have added logic like below

SET "Offertime=%time:~0,2%"

IF %Offertime% leq 23  set  "RunOffer=True"
IF %Offertime% geq 07  set  "RunOffer=True"


IF "%RunOffer%"=="True" (

CALL "C:\TCC_Touch_Point_Folder\bin\Windows\core\Rcap_Offer_Cal.bat"
 )

exit

But this is not working, can anyone please let me know where I am going wrong in this ? or any better way to achieve this solution?

Your condition is always true because 'Offertime' is either less/equal than 23 or greater/equal than 7. This would work:

SET hour=%time:~0,2%

SET shouldrun=True
IF %hour% geq 23 SET shouldrun=False
IF %hour% leq 6 SET shouldrun=False

IF "%shouldrun%"=="True" (
    CALL "C:\TCC_Touch_Point_Folder\bin\Windows\core\Rcap_Offer_Cal.bat"
)

If you're looking to run a bat file at a specific time, the usual way is to schedule it using windows task scheduler.

  • Click Start
  • Type Task and then click Task Scheduler

or

  • Click Start
  • Click All Programs
  • Click Accessories > System Tools > Task Scheduler

Then create a task setting up the time to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM