【问题标题】:How can I detect if there is any file in a folder using SSIS?如何使用 SSIS 检测文件夹中是否有任何文件?
【发布时间】:2020-03-14 07:33:52
【问题描述】:

我每天都会收到一个具有特定模式和扩展名的文件,我想在特定进程下运行该文件。我想检查我的文件夹中是否有任何文件,否则我将执行另一项任务。到目前为止,我发现您可以使用脚本任务并执行 File.Exist。但是,我做错了,因为它没有将 * 作为通配符。 Devoluciones_source 是“C:\Users\us1\Folder\” FileToSearch 是“返回”

我的文件: return_20200102.csv return_20200203.csv

String Filepath = Dts.Variables["$Project::Devoluciones_source"].Value.ToString() + Dts.Variables["User::FileToSearch"].Value.ToString() + "*csv";

        if (
            File.Exists(Filepath))

        {
            Dts.Variables["User::IsFound"].Value = 1;
        }

【问题讨论】:

  • 你试过*.csv

标签: c# script-task ssis-2017


【解决方案1】:

我认为 File.Exits() 不接受通配符,它​​会检查文字文件路径并返回 false,因为找不到 C:\Users\us1\Folder\*.csv

您可以做的是获取文件夹中的文件 C:\Users\us1\Folder\ 并使用 Directory.GetFiles(path, searchPattern) 再次检查这些文件的搜索模式

像这样:

string dirPath = Dts.Variables["$Project::Devoluciones_source"].Value.ToString(); 
string fileName = Dts.Variables["User::FileToSearch"].Value.ToString();

// if you want to make sure the directory exists:
if(Directory.Exists(dirPath) {
    string[] files = Directory.GetFiles(dirPath, fileName + "*.csv");

    if(files.lenght > 0) {
        // you can now iterate over each file that is found in the users directory that matches your pattern and do your logic.
    }
}

有关 Directory.GetFiles 方法的更多信息:Directory.GetFiles on docs.Microsoft.com

有关 Files.Exists 方法的更多信息:Directory.GetFiles on docs.Microsoft.com

【讨论】:

    猜你喜欢
    • 2014-07-09
    • 2013-07-08
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 2018-11-17
    • 2011-10-09
    相关资源
    最近更新 更多