简体   繁体   中英

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

I want to check to see if a file exists in a particular folder from SSIS. How can I accomplish this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM