How to execute commands in a batch file in timed intervals

Updated: 05/21/2018 by Computer Hope
Batch file

The solution for executing a batch file or commands in a batch file may change depending on what best fits your needs. Also, the version of Windows or MS-DOS matters.

First solution

In the example below, the batch file is placed into a loop and executes the "dir" command every 20 seconds. This solution would be best for users who need to execute a command frequently.

:START
REM Execute the MS-DOS dir command ever 20 seconds.
dir
SLEEP 20
GOTO END
Note

For the above batch file to run properly, you must have the sleep MS-DOS utility on the computer. This utility is not included with any version of MS-DOS or Windows. However, once downloaded allows your computer to sleep or delay for any specified amount of seconds. See the utility downloads page for a download link.

Second solution

Our next solution is for users who need to execute a batch file but only need to execute the command every few hours, days, months, or years. Instead of relying on the batch file continuously running, we recommend using a scheduling utility that will execute the batch file when the date or time arrives.

Microsoft Windows NT, 2000, and XP users, for example, can use the AT command to schedule timed events through MS-DOS.