-4

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

4

1 回答 1

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;
}
于 2012-11-02T08:06:36.133 回答