Task
Question
Is it possible to monitor a directory like c:\tmp\jobscheduler\files\incoming
and look for strings in text files that then trigger a job to start a Microsoft Access macro with the string found in that file?
Example
Looking for string in file like “FF040614CA
” and then start C:\Program Files (x86)\Microsoft Office\OFFICE11\MSACCESS.exe q:\companyX\FF040614CA\U4284.mdb /x mkrU4284
Solution
Download
- Download the attached archive: file.zip
- Unzip the archive to the
live
folder of your JobScheduler installation
Components
- A job chain that monitors the directory for incoming files
- Two jobs that process incoming files in sequence:
- Check the file for a string: if the string is available then the job chain is continued otherwise it is terminated.
- Start MS Access with the matching string found from the file
Configuration
Job Chain: watch_files
Basically you would set-up a job chain that monitors the directory for incoming files like this:
Explanations
- The file order souce is configured to use a regular expression that ignores files that would not match the expression.
- The incoming file has to be moved from the incoming directory until the end of the job chain. Two
success
anderror
nodes are added that move the file into different directories depending on the processing result.
Job: check_file_for_string
The following job implements checking of a file for a string:
<job order="yes" stop_on_error="no" name="check_file_for_string"> <script language="shell"> <![CDATA[ @echo environment variable for incoming file: SCHEDULER_PARAM_SCHEDULER_FILE_PATH=%SCHEDULER_PARAM_SCHEDULER_FILE_PATH% @echo environment variable for return values: SCHEDULER_RETURN_VALUES=%SCHEDULER_RETURN_VALUES% powershell.exe -Command "& { $match = Select-String -Path $env:SCHEDULER_PARAM_SCHEDULER_FILE_PATH -Pattern 'FF[0-9]*CA' | Select Matches; if ($match) { 'match=' + $match.Matches[0].Value | out-file -append $env:SCHEDULER_RETURN_VALUES -encoding Default; exit(0) } else { exit(1) } }" ]]> </script> <run_time /> </job>
Explanations
- JobScheduler provides parameters as environment variables to shell jobs. Two variables are used:
SCHEDULER_PARAM_SCHEDULER_FILE_PATH
: for a file order source JobScheduler provides this variable that contains the file pathSCHEDULER_RETURN_VALUES
: JobScheduler provides this variable that contains the name of a temporary file. Shell jobs could write pairs of names and values to the file that would become parameters for subsequent jobs.- The basic shell syntax is:
echo my_param=my_value>>%SCHEDULER_RETURN_VALUES%
- The PowerShell syntax is:
'my_param=my_value' | out-file -append $env:SCHEDULER_RETURN_VALUES
- The PowerShell syntax used in the job is:
'match=' + $match.Matches[0].Value | out-file -append $env:SCHEDULER_RETURN_VALUES
This syntax creates a parameter with the namematch
and the value from the matching string found in the incoming file.
- The basic shell syntax is:
- PowerShell is used in order to parse the incoming file for a regular expression. If a match is found then it is added as a parameter with the name
match
for subsequent jobs.
Job: start_msaccess
The following job makes use of a previously found string match and would execute MS Access with a macro accordingly:
<job order="yes" stop_on_error="no" title="Start MS Access" name="start_msaccess"> <script language="shell"> <![CDATA[ @echo ... starting MS Access @echo environment variable for match from previous job: SCHEDULER_PARAM_MATCH=%SCHEDULER_PARAM_MATCH% @echo "C:\Program Files (x86)\Microsoft Office\OFFICE11\MSACCESS.exe" q:\companyX\%SCHEDULER_PARAM_MATCH%\U4284.mdb /x mkrU4284 ]]> </script> <run_time /> </job>
Explanations
- In the previous job a parameter
match
has been created that is provided by the environment variable%SCHEDULER_PARAM_MATCH%
to the current job. - The job uses this variable to parameterize the launch of MS Access