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):

REM NOTE THAT 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.
   Gert

Friday, 5 September 2008

Automatically add a header page to new PDF documents

The example batch script below will combine 2 PDFs using the pdftk program (Auf Deutsch).
One of the PDF files is a "static" page that will be used as the "header" for the output PDF. The other PDF file is the file detected by WatchDirectory.

Download pdftk

You can download the Windows version of pdftk here:
http://www.accesspdf.com/article.php/20041130153545577

WatchDirectory task

Create a new task and select the run a batch file plugin. Select the sample batch script below (make sure you changed its settings the way you need). Make sure you only select the "FILENEW" option on the Events page.

The example batch script

Below is the example script. Please make sure to change the 3 settings starting on line 5 to the correct location of pdftk, the header.pdf you want to use and the output directory.
(The script does not show properly in some blog-readers, please copy it from the original post)

If you want the script to automatically delete the original PDF, remove the REM on the last line.

A small variation

If you want to add the static page as the last page of the new PDF, change line 15 from (original):

"%PD%" "%HDP%" "%WD_FILE%" cat output "%OUTDIR%\%WD_FILE_N%"

to (new):

"%PD%" "%WD_FILE%" "%HDP%" cat output "%OUTDIR%\%WD_FILE_N%"