Q:

Enumerate files in a folder using SSIS Script Task

Can someone please show how to enumerate files in a given folder using Script Task in SSIS?

A:

Configure two variables: read-only string User::download_path and read-write object User::files_to_process to receive the list of files.
public void Main()
{
    bool fireAgain = true;

    var filesToProcess = new System.Collections.ArrayList();
    var filesInDirectory = new System.Collections.ArrayList();

    var download_path = (String)Dts.Variables["User::download_path"].Value;

    // Find for example all csv files in the directory which are having size > 0 
    var downloadedFiles = new DirectoryInfo(download_path).EnumerateFiles("*.csv",SearchOption.TopDirectoryOnly);
    foreach (var f in downloadedFiles)
    {
        if (f.Length > 0)
            filesInDirectory.Add(f.FullName);
    }

    Dts.Events.FireInformation(3, "Getting files in directory", downloadedFiles.Count().ToString() + " found.", "", 0, ref fireAgain);

    // Report the file names into the SSIS Log:
    foreach (var f in filesToProcess)
    {
        Dts.Events.FireInformation(3, f.ToString(), "Ready for processing", "", 0, ref fireAgain);
    }

    // Return them into the READWRITE object variable
    Dts.Variables["User::files_to_process"].Value = filesInDirectory;

    Dts.TaskResult = (int)ScriptResults.Success;
}

