Summary

This article demonstrates how to read data from the file that triggered a job and put that data into a Job Variable for subsequent use in the job.

Scenario

In this example, the file that triggers the job is a text file that contains a year value that needs to be passed to a program run by the job.

In our example, the trigger file is a text file that contains a single line in the form:

year=2015

(where "2015" could be any year). When the job is triggered, it will read the file, parse out the year value and assign it to a Job Variable, then run another program, passing the year (from the Job Variable) on the command line. 

The job contains the following key elements:

  • A File Trigger to trigger the job when the file is created or modified.
  • A Script Execution Task to read the year value from the trigger file and assign it to a Job Variable.
  • A Program Execution Task to run the target program, passing it the year value.

Trigger Configuration

Add a File Trigger to the job, configured as appropriate. Be sure to select the "Trigger a separate instance for each matching file" option for the Trigger Mode.

On the Triggers page of the Job Properties, select "Execute (start a new instance)" for the Multiple Instances setting.

Script Configuration

The script will read the contents of the trigger file and look for the year. It will take just the digits of the year (ignoring the "year=" part) and assign that value to a Job Variable named "TriggerYear."

Add a new step to the job to execute a Script Execution Task. If the job already has other steps, move this step up to be step 1 after you finish creating it.

In the Script Execution Task Properties window, select the option to "Execute a script stored in adTempus" and click New to create a new script.

In the Script Properties window, make sure the Language is set to "VB.NET" and replace the template script code with the following code:

Imports System
Imports System.Collections.Generic
Imports ArcanaDevelopment.adTempus.Shared
Imports ArcanaDevelopment.adTempus.ApplicationIntegration
Imports System.IO
Imports System.Text.RegularExpressions
 
Public Class UserScript
    Inherits ArcanaDevelopment.adTempus.ApplicationIntegration.UserScriptBase
 
    Public Overrides Function Run() As Object
 
        'get the filename passed by the trigger
        Dim fileName=adTempus.JobVariables("FileTrigger.FileName")
         
        'check to make sure file exists
        If Not File.Exists(fileName) Then
            adtempus.LogMessage(MessageTypeEnum.Error, 0, "Trigger file not found: " & fileName)
            'return 8 to indicate failure
            Return 8
        End If
         
        'Open the file
        Using reader=File.OpenText(fileName)
            'read all the contents
            Dim content=reader.ReadToEnd()
         
            'use a Regular Expression to extract the year
            'This Regular Expression looks for any text that matches the pattern
            '   year=YY
            'where YY is 2 or more digits
            Dim match=RegEx.Match(content,"year=(\d{2,})",RegExOptions.IgnoreCase)
            If Not match.Success Then
                adtempus.LogMessage(MessageTypeEnum.Error, 0, "Trigger file """ & fileName & """ not formatted correctly: year not found")
                'return 8 to indicate failure
                Return 8
            End If
             
            'store the year in the TriggerYear variable
            adTempus.JobVariables.Add("TriggerYear",match.Groups(1).Value,False)
        End Using
 
        Return 0
    End Function
End Class

This script was written for a specific need (extracting a 2 or 4 digit year from the file) but can easily be adapted to look for a different pattern in the file, or to place the full file contents into the variable.

Click OK to save the script, then OK again to save the step.

Program Execution Configuration

After the script above runs as step 1 of the job, the "TriggerYear" Job Variable will contain the year value read from the file. (Note that the variable is only available within the current instance of the job; it's not available for other jobs.)

To pass this value to the program being run by the job, add a step that executes a Program Execution Task to run the program. In the Command-Line Parameters box of the Program Execution Task Properties, include any parameters that need to be passed to the program. To insert the year value from the file, use the token "%TriggerYear%".

For example, the parameters might look like this:

-generatereport -year %TriggerYear%

When the job runs, "%TriggerYear%" will be replaced with the value read from the file.