使用SSIS脚本任务枚举文件夹中的文件

时间:2012-11-02 04:58:43

标签: ssis

有人可以使用SSIS中的脚本任务来展示如何枚举给定文件夹中的文件吗?

1 个答案:

答案 0 :(得分:1)

配置两个变量:只读字符串User::download_path和读写对象User::files_to_process以接收文件列表。

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;
}