Loading...

April 29, 2008

Working with scheduled tasks from the command prompt in Windows

When I am working on my laptop I am in different locations and in a different context. During the day I am at work, connected to the network at work. In the evening and at night I am at home, connected to my home network. I have created a simple batch file to setup my different environments.

One of the things I needed to do was to setup a automated task when I am at work, but not when I am at home. Therefore I needed to create and delete tasks from my batch file. First I used AT, because I was used to it. But in Vista AT is available, but it didn't work for me. But the successor to AT is schtasks, which is much more powerful than AT.

Typing schtasks gives a brief overview of the options. We have options like /Create, /Query and /Delete. To get more information we simply type schtasks /Create /? to get more help. We get a decent amount of information and examples to get started. I was suprised to see the amount of options available. One of the options is /TN to set the taskname. The value of this options must be a valid filename, so we cannot use characters which are invalid for filenames.

I adjusted my batch file to first delete any scheduled task I created before. And then after selecting a location, creating the correct tasks:

rem Delete previously created task.
schtasks /Delete /F /TN Update_CVS

:LocationSettings
rem Create new task.
schtasks /Create /F /TN Update_CVS \
/SC Once /ST 08:00 /TR cvs -Q update -dP