I want to check to see if a file exists in a particular folder from SSIS. How can I accomplish this?
我想检查SSIS中特定文件夹中是否存在文件。我怎么能做到这一点?
4 个解决方案
#1
10
Variables:
变量:
folder - string - C::\Temp\
folder - string - C :: \ Temp \
file - string - 1.txt
file - string - 1.txt
fileExists - boolean - False
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;
}
#2
6
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 :)
您可以使用Foreach循环容器,只需将所有项目放入其中即可。如果文件存在则执行,否则不执行。很简单 :)
#3
1
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
http://sqlmag.com/sql-server-integration-services/simple-effective-way-tell-whether-file-exists-using-ssis-package
#4
1
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.)
作为具有“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;
}
}
#1
10
Variables:
变量:
folder - string - C::\Temp\
folder - string - C :: \ Temp \
file - string - 1.txt
file - string - 1.txt
fileExists - boolean - False
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;
}
#2
6
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 :)
您可以使用Foreach循环容器,只需将所有项目放入其中即可。如果文件存在则执行,否则不执行。很简单 :)
#3
1
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
http://sqlmag.com/sql-server-integration-services/simple-effective-way-tell-whether-file-exists-using-ssis-package
#4
1
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.)
作为具有“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;
}
}