Search code examples
windowsbatch-filecmd

Execute batch script once a day only


I am new at working with the batch scripts and I have got a scenario which i have to take care very soon. I have a batch script that is responsible to send sms to the users. I want this batch script to run once a day only no matter how many times I try to execute it. Thanks in advance


Solution

  • I am curious. Why would you ask users to run the batch file? You should look at scheduling commands using SCHTASKS, the Task Scheduler on Windows 10.

    See: https://www.windowscentral.com/how-create-task-using-task-scheduler-command-prompt

    If you insist on user run batch file, you have to use some kind of persistent run-once flag. You could implement something using the following logic:

    1. Check for existence of a file named .runonce-yyyymmdd in a specific directory
    2. If the file doesnt exist, create it. Contents doesnt matter. A zero byte file is fine. a. Delete the previous day's .runonce-yyyymmdd file if it exists. b. Proceed with execution of the rest of the batch file.
    3. If the file exists, stop (because the batch file has already run for the day).