SSIS Script task to check if file exists in folder or not

纵然是瞬间 提交于 2019-11-27 22:21:21

Variables:

folder - string - C::\Temp\

file - string - 1.txt

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

You can use Foreach Loop Container and simply place all your items into it. It will be executed if file exists and won't if not. Very simple :)

As an alternative to having an "out" variable, you could also Change the Dts.TaskResult based on whether or not the file exists. The snippet below fails the script task if the file doesn't exist. (It also creates a log entry if logging is enabled.)

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

}

There are no native tasks inside SSIS that can do this check but you can accomplish this using a Script Task but i suggest you check the following links for simple steps required to achieve that.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!