使用 SSIS 脚本任务枚举文件夹中的文件 [英] Enumerate files in a folder using SSIS Script Task

查看:47
本文介绍了使用 SSIS 脚本任务枚举文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于使用 SSIS 脚本任务枚举文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆