C# 用于检查文件是否存在于文件夹中的 SSIS 脚本任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17568554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SSIS Script task to check if file exists in folder or not
提问by user1429135
I want to check to see if a file exists in a particular folder from SSIS. How can I accomplish this?
我想检查一个文件是否存在于 SSIS 的特定文件夹中。我怎样才能做到这一点?
回答by StackTrace
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.
SSIS 中没有可以执行此检查的本机任务,但您可以使用脚本任务完成此操作,但我建议您检查以下链接以了解实现该目标所需的简单步骤。
http://www.bidn.com/blogs/DevinKnight/ssis/76/does-file-exist-check-in-ssis
http://www.bidn.com/blogs/DevinKnight/ssis/76/does-file-exist-check-in-ssis
回答by Anoop Verma
Variables:
变量:
folder - string - C::\Temp\
文件夹 - 字符串 - C::\Temp\
file - string - 1.txt
文件 - 字符串 - 1.txt
fileExists - boolean - False
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;
}
回答by RubberDuck
As an alternative to having an "out" variable, you could also Change the Dts.TaskResultbased 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.)
作为拥有“out”变量的替代方法,您还可以Dts.TaskResult根据文件是否存在来更改。如果文件不存在,下面的代码片段会使脚本任务失败。(如果启用了日志记录,它还会创建一个日志条目。)
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;
}
}
回答by Andrey Morozov
You can use Foreach Loop Containerand simply place all your items into it. It will be executed if file exists and won't if not. Very simple :)
您可以使用Foreach Loop Container并简单地将所有物品放入其中。如果文件存在则执行,否则不执行。很简单 :)

