Search This Blog


Wednesday, 10 September 2008

Automatically start WatchDirectory tasks

WatchDirectory has a Scheduler that can be used to enable or disable the monitoring part (looking for new/changed files) of a task. This post will show you a way to start or stop the task completely and (more important) automatically.

Starting a task from the commandline

The wdrun.exe program (the program that actually "runs" the task) has several commandline options that can be used to start or stop a task. For example, to start the task named Hello, you enter the following command in the Windows "Run..." menu or a command box (cmd.exe):

"C:\Program Files\watchDirectory\wdrun.exe" -start Hello

If the "Hello" task was already running, no bad things happen. To stop the task:

"C:\Program Files\watchDirectory\wdrun.exe" -stop Hello

This will request the task to stop. Even though the command returns immediately, it can take a few seconds before the task is actually stopped.

Starting tasks automatically

Lets assume you have 3 tasks (called T1, T2 and T3) that you want to start automatically. For simplicity, I assume these tasks are configured to run as a Windows Service. Create a batch file (for example called C:\Bin\Start123.bat) with the following content:

"C:\Program Files\watchDirectory\wdrun.exe" -start T1
"C:\Program Files\watchDirectory\wdrun.exe" -start T2
"C:\Program Files\watchDirectory\wdrun.exe" -start T3

Now find the Windows "Scheduled Tasks" applet on the Windows Control panel (its exact location depends on your Windows version). Create a new Scheduled task, and configure it to run this batch file. The Windows Scheduler has flexible options to set the days and times when the Scheduled Task should run.

Obviously, you can create another batch file to stop these tasks (replace -start with -stop), I will leave that as an exercise for the reader.

If the task is not running as a Windows Service

The batch file above will not work properly if the WatchDirectory tasks are configured to run as a "normal" program. What happens is that only task T1 will run, because the "-start T1" will actually run the task. This causes the batch file to "hang" on the first command. To start the task in another window so the rest of the batch file will run, we need to use the Windows "START" command.
First, I will show you how NOT to do it (this batch file does NOT work):

start "C:\Program Files\watchDirectory\wdrun.exe" -start T1
start "C:\Program Files\watchDirectory\wdrun.exe" -start T2
start "C:\Program Files\watchDirectory\wdrun.exe" -start T3

The reason it doesn't work is because the Windows "Start" command will interpret
"C:\Program Files\watchDirectory\wdrun.exe"
to be the Title of the new window. So, even if we are not interested to give the window a title, we must supply one. Here is a batch file that does work:

start "task 1" "C:\Program Files\watchDirectory\wdrun.exe" -start T1
start "task 2" "C:\Program Files\watchDirectory\wdrun.exe" -start T2
start "task 3" "C:\Program Files\watchDirectory\wdrun.exe" -start T3

The "Start" command has several other possible options, for example you let it open the new window minimized like this:

start "task 1" /MIN "C:\Program Files\watchDirectory\wdrun.exe" -start T1

Hope this helps.

No comments: