Summary

This article demonstrates how to use a script to dynamically set the list of files to be processed by a File Operation task (file copy, move, delete, or compress).

Scenario

The File Operation tasks allow you to specify a list of files to process at configuration time, or to use wildcards to select files. The tasks also allow you to use Job Variables to specify the files to process, to support situations where you want to provide an explicit list of files to process, at runtime.

In this example we assume that a previous step in the job (or some external process) has generated a list of the files that need to be transferred using an FTP task. That list of files is stored in a text file, with one file name per line. The script reads the file and builds a comma-separated list of file names, which is stored in a Job Variable to be used by the task.

Implementation

1. Add a new step to your job, ahead of the File Transfer step. Select the option to Execute a Script.

2. In the Script Execution Task Properties window, click New in the Execute a step stored in adTempus section.

3. In the Script Properties window, change the script language to "C#" and replace the template script code with the following:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using ArcanaDevelopment.adTempus.Shared;
using ArcanaDevelopment.adTempus.ApplicationIntegration;

public class UserScript : ArcanaDevelopment.adTempus.ApplicationIntegration.UserScriptBase
{
    public override object Run()
    {

        // reads a list of file names from a file, creates a comma-delimited list, and sets the FilesToProcess variable from the list.
        string fileList = "";

        //Replace the file name as appropriate, or read it from a Job Variable.
        using (var source = File.OpenText(@"c:\test\filelist.txt"))
        {
            var line = source.ReadLine();
            while (line != null)
            {
                if (!string.IsNullOrWhiteSpace(line))
                {
                    if (fileList.Length > 0)
                        fileList = fileList + ",";
                    fileList = fileList + line.Trim();
                }
                line = source.ReadLine();
            }

            //put the list in the FilesToProcess variable to use later
            adTempus.JobVariables.Add("FilesToProcess", fileList, false);
        }

        return 0;
    }
}

4. Save the script and step.

5. Edit (or create) the step to perform the file transfer.

6. In the File Transfer Task Properties window, set the Include Files to "%FilesToProcess%":

7. Configure the rest of the settings for the transfer task as appropriate.

When the job runs, the script creates the variable with the list of files. The file transfer task reads the variable and processes the file names it contains.