How to schedule or periodically loop tasks in Windows

For scheduling in Windows, there are classy options like Task Scheduler and running programmes on startup. Also, it worth mention that you can create a loop in a batch file to periodically run some command line commands, R scripts or other programmes that are possible to run in batch mode.

Run Batch file with Task Scheduler (Windows 10)

When you are creating a new task in the Task Scheduler go to the Actions tab, create a new action and fill two fields:
1. Program/script – name of the Batch file.
2. Start in (optional) – with a path to Batch file.

Open Excel file with Task Scheduler (Windows 10)

It comes handy especially when your Excel file contains macros that run when opening a workbook. When you are creating a new task in the Task Scheduler go to the Actions tab, create a new action and fill two fields:
1. Program/script – with a path to EXCEL.EXE. If there are spaces in pats quotation marks needed. In my case C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE
2. Add arguments (optional) – with a path to Excel file. In my case C:\Users\somebody\Desktop\OP_INFO_AUTO.xlsm

Run program on Windows startup

For example, if you need to run a batch file only on Windows startup, then you can use the Startup folder.
1. Create a shortcut to file.
2. Press Windows key+R to launch Run window and type shell:startup to open Startup folder.

3. Move your shortcut to the Startup folder.

CMD loop and timeout

Here is an example code with loop and timeout which is needed to have some delay in code execution.
For example, I’m running code after every 60 seconds.

@echo off

:loop

echo Here is your additional code.

timeout /T 60 /NOBREAK

goto loop

Or R script with a 60-second timeout.

@echo off

:loop

"C:\Program Files\R\R-3.4.3\bin\R.exe" CMD BATCH C:\Users\myusername\Documents\R\Send_Outlook_Email.R

timeout /T 60 /NOBREAK

goto loop

In some way, it is the same as Task Scheduler that periodically runs the necessary code. If you have to figure out something more about timeout functions, what is the meaning of used parameters, then type timeout /? in the command line.

In one of the previous posts, you can see how to run R script from command line and with Task Scheduler or Windows command line loop and timeouts you can run them periodically.

If you need a good command line alternative, then try out PowerShell. There is also very helpful command Sleep that works similarly like Timeout in CMD.




Posted

in

,

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *