# Trigger file processing

Use this scenario when files from a source location should be processed only after a specific trigger file becomes available. A trigger file is commonly used to indicate that a file delivery is complete and that processing can begin.

This example demonstrates a simple trigger file workflow in which the trigger file is used only to indicate that processing should continue to the next step in the task.

## How It Works

The task repeatedly checks for the presence of a trigger file.

The **Repeat** step exits successfully when either:

* The trigger file is found.
* The configured time limit is reached.

After the Repeat step exits, a **Custom Script** verifies that the trigger file was found.

* If the trigger file was detected, task execution continues.
* If the trigger file was not detected before the time limit expired, the task fails.

## Configure the Task

The filename `trigger.txt` is used for illustration purposes only. You can use any filename that matches your workflow requirements.

1. Add a **Repeat** step to the task.

2. Within the Repeat step, add a **Download** step that points to the source location and uses the following file filter:

   `trigger.txt`

3. Configure the Repeat step to exit when either of the following conditions is met:

   * **1 hour** has elapsed.
   * **1 file** has been found.

   The Repeat step completes successfully when either condition is reached.

4. Add a **Custom Script** step immediately after the Repeat step and enter the following PowerShell script:

   ```powershell
   $items = @(Get-MftItem)

   if ($items.Count -eq 0) {
       throw "End the task"
   }
   ```

   The script checks whether any files were returned by the Repeat step.

   * If `trigger.txt` is found, the task continues to the next step.
   * If the Repeat step exits because the **1-hour timeout** is reached and no files are found, the script throws an error and the task fails.

5. Add another **Download** step and configure it to download files from the source location using the following file filter:

   `!trigger.txt`

   This downloads the files to be processed while excluding the trigger file.

6. Add an **Upload** step and configure it to upload the downloaded files using the following file filter:

   `!trigger.txt`

   This uploads the processed files while excluding `trigger.txt`.

## Advanced trigger file processing

In some workflows, the trigger file contains a list of files or file patterns that should be processed. Instead of using the trigger file only as an indicator that processing can begin, you can use the trigger file contents to determine which files are downloaded from the source location.

In this scenario, the task waits for a trigger file, reads its contents, and stores those contents in a task parameter. The parameter can then be used as the file specification in a subsequent source step.

For example, the trigger file might contain:

```
file1.txt
file2.txt
```

Replace the script used in Step 4 above with the following script:

```powershell
# gets any files in cache and assumes they are trigger files
# will concatenate contents of all files into a parameter named 'FileSpec'
# the FileSpec parameter will be used later in the task as content for the
# file specification field
#
# trigger file should have content that looks like:
# file1.txt
# file2.txt
# ...
#
# (or any file specification that works in a standard download file spec step)

$items = Get-MftItem
$triggerfilecount = 0

# for every trigger file found, read contents and add it to $FileSpec variable
foreach ($item in $items) {
    $FileSpec += (Get-MftContent -Item $item)
    $triggerfilecount++
}

if ($triggerfilecount -eq 0) {
    # if we didn't find a trigger file, put task into error state
    # you can define the error text as you want and it will be preserved
    # in the [ErrorDescriptionTask] parameter
    Throw "No Trigger file"
}
else {
    Set-MftParameter -Name FileSpec -Value $FileSpec
}
```

After the script runs, use the `FileSpec` task parameter as the file specification in the source step. The task then downloads only the files identified by the trigger file contents.
