How to execute commands in a batch file in timed intervals

The solution for executing a batch file or commands in a batch file may change depending on what best fits your needs and what version of Windows or MS-DOS you're using.

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: In order 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 download 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 that a user utilize an 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.