【问题标题】:SSIS Script task to check if file exists in folder or notSSIS脚本任务检查文件夹中是否存在文件
【发布时间】:2013-07-08 06:13:53
【问题描述】:

我想检查某个文件是否存在于 SSIS 的特定文件夹中。我怎样才能做到这一点?

【问题讨论】:

  • 您可以以此为起点。 stackoverflow.com/questions/7385251/…
  • 我正在使用下面的代码 Dts.Variables("FileExists").Value = File.Exists(Dts.Variables("FileLocation").Value) 其中 FileExists 是一个布尔变量和“FileLocation " 是一个带有文件路径的字符串变量虽然文件存在于文件夹中,但它仍然给出 False 值。
  • 只是好奇下面给出的解决方案是否对您有用,以及您是否能够解决您的问题。您的反馈将对我以及未来遇到类似问题的访问者有所帮助。谢谢。

标签: c# file ssis file-exists


【解决方案1】:

变量:

文件夹 - 字符串 - C::\Temp\

文件 - 字符串 - 1.txt

fileExists - 布尔值 - False

public void Main()
{
    string folder = Dts.Variables["User::folder"].Value.ToString();     //@"C:\temp\";
    string file = Dts.Variables["User::file"].Value.ToString();         //"a.txt";
    string fullPath = string.Format(@"{0}\{1}", folder, file);

    Dts.Variables["User::fileExists"].Value = File.Exists(fullPath);

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

【讨论】:

  • 我尝试了上面的答案,但是在我们的环境中运行 ssis 时出现错误。目标调用引发了异常。
  • 请确保将这些变量传递给脚本;文件夹和文件为只读,文件存在为读写
【解决方案2】:

您可以使用Foreach Loop Container 并将所有物品放入其中。如果文件存在则执行,不存在则不执行。很简单:)

【讨论】:

  • 非常好!我一直在寻找简单且易于实现且运行良好的东西。
【解决方案3】:

作为“out”变量的替代方法,您还可以根据文件是否存在更改Dts.TaskResult。如果文件不存在,则下面的 sn-p 会使脚本任务失败。 (如果启用了日志记录,它还会创建一个日志条目。)

public void Main()
{
    string fileName = Dts.Variables["User::sourcePath"].Value.ToString() + Dts.Variables["User::fileName"].Value.ToString();

    if (File.Exists(fileName))
    {
        Dts.TaskResult = (int)ScriptResults.Success;
    } 
    else 
    {
        Dts.Log(string.Format("File {0} was not found.",fileName),0,null);
        Dts.TaskResult = (int)ScriptResults.Failure;
    }

}

【讨论】:

    【解决方案4】:

    SSIS 内没有可以执行此检查的本机任务,但您可以使用脚本任务完成此操作,但我建议您查看以下链接以了解实现此操作所需的简单步骤。

    http://www.bidn.com/blogs/DevinKnight/ssis/76/does-file-exist-check-in-ssis

    http://sqlmag.com/sql-server-integration-services/simple-effective-way-tell-whether-file-exists-using-ssis-package

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      相关资源
      最近更新 更多