我知道您已经找到了问题的答案。这是针对可能偶然发现此问题的其他用户的。这是实现这一目标的一种可能方法。Script Task
可用于查找给定模式的 FTP 文件夹路径中存在的文件列表(例如*.txt
)。下面的示例展示了如何做到这一点。
分步过程:
在 SSIS 包上创建一个FTP Connection
named FTP并还创建5变量如屏幕截图所示#1。多变的RemotePath
包含 FTP 文件夹路径;LocalPath
包含文件将下载到的文件夹;FilePattern
包含文件模式以查找要从 FTP 服务器下载的文件列表;FileName
将由以下人员填充Foreach loop container
但为了避免 FTP 任务设计时错误,可以将其填充为/ or the DelayValidation
FTP 任务上的属性可以设置为True.
在 SSIS 包上,放置一个Script Task
, Foreach Loop container
and FTP Task
内Foreach Loop container
如截图所示#2.
更换Main()
内的方法Script Task
与下面的代码脚本任务代码部分。脚本任务将填充变量文件列表与匹配给定模式的文件集合。此示例将首先使用模式 *.txt,该模式不会产生任何结果,然后使用模式 *.xls,该模式将匹配 FTP 服务器上的几个文件。
配置Foreach Loop container
如截图所示#3 and #4。此任务将循环访问变量 **ListOfFiles*。如果没有文件,循环容器内的FTP任务将不会执行。如果有文件,则循环容器内的 FTP 任务将执行在 FTP 服务器上找到的文件数量的任务。
配置FTP Task
如截图所示#5 and #6.
截屏 #7显示示例包执行情况no找到该模式的匹配文件*.txt
.
截屏 #8显示文件夹的内容C:\temp\
before包的执行。
截屏 #9显示找到该模式的匹配文件时的示例包执行情况*.xls
.
截屏 #10显示 FTP 远程路径的内容/Practice/Directory_New
.
截屏 #11显示文件夹的内容C:\temp\
after包的执行。
截屏 #12当提供不正确的信息时显示包失败远程路径.
截屏 #13显示与包失败相关的错误消息。
希望有帮助。
脚本任务代码:
C#可以使用的代码SSIS 2008 and above
.
包括using
陈述使用 System.Text.RegularExpressions;
public void Main()
{
Variables varCollection = null;
ConnectionManager ftpManager = null;
FtpClientConnection ftpConnection = null;
string[] fileNames = null;
string[] folderNames = null;
System.Collections.ArrayList listOfFiles = null;
string remotePath = string.Empty;
string filePattern = string.Empty;
Regex regexp;
int counter;
Dts.VariableDispenser.LockForWrite("User::RemotePath");
Dts.VariableDispenser.LockForWrite("User::FilePattern");
Dts.VariableDispenser.LockForWrite("User::ListOfFiles");
Dts.VariableDispenser.GetVariables(ref varCollection);
try
{
remotePath = varCollection["User::RemotePath"].Value.ToString();
filePattern = varCollection["User::FilePattern"].Value.ToString();
ftpManager = Dts.Connections["FTP"];
ftpConnection = new FtpClientConnection(ftpManager.AcquireConnection(null));
ftpConnection.Connect();
ftpConnection.SetWorkingDirectory(remotePath);
ftpConnection.GetListing(out folderNames, out fileNames);
ftpConnection.Close();
listOfFiles = new System.Collections.ArrayList();
if (fileNames != null)
{
regexp = new Regex("^" + filePattern + "$");
for (counter = 0; counter <= fileNames.GetUpperBound(0); counter++)
{
if (regexp.IsMatch(fileNames[counter]))
{
listOfFiles.Add(remotePath + fileNames[counter]);
}
}
}
varCollection["User::ListOfFiles"].Value = listOfFiles;
}
catch (Exception ex)
{
Dts.Events.FireError(-1, string.Empty, ex.ToString(), string.Empty, 0);
Dts.TaskResult = (int) ScriptResults.Failure;
}
finally
{
varCollection.Unlock();
ftpConnection = null;
ftpManager = null;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
VB可以使用的代码SSIS 2005 and above
.
包括Imports
陈述导入 System.Text.RegularExpressions
Public Sub Main()
Dim varCollection As Variables = Nothing
Dim ftpManager As ConnectionManager = Nothing
Dim ftpConnection As FtpClientConnection = Nothing
Dim fileNames() As String = Nothing
Dim folderNames() As String = Nothing
Dim listOfFiles As Collections.ArrayList
Dim remotePath As String = String.Empty
Dim filePattern As String = String.Empty
Dim regexp As Regex
Dim counter As Integer
Dts.VariableDispenser.LockForRead("User::RemotePath")
Dts.VariableDispenser.LockForRead("User::FilePattern")
Dts.VariableDispenser.LockForWrite("User::ListOfFiles")
Dts.VariableDispenser.GetVariables(varCollection)
Try
remotePath = varCollection("User::RemotePath").Value.ToString()
filePattern = varCollection("User::FilePattern").Value.ToString()
ftpManager = Dts.Connections("FTP")
ftpConnection = New FtpClientConnection(ftpManager.AcquireConnection(Nothing))
ftpConnection.Connect()
ftpConnection.SetWorkingDirectory(remotePath)
ftpConnection.GetListing(folderNames, fileNames)
ftpConnection.Close()
listOfFiles = New Collections.ArrayList()
If fileNames IsNot Nothing Then
regexp = New Regex("^" & filePattern & "$")
For counter = 0 To fileNames.GetUpperBound(0)
If regexp.IsMatch(fileNames(counter)) Then
listOfFiles.Add(remotePath & fileNames(counter))
End If
Next counter
End If
varCollection("User::ListOfFiles").Value = listOfFiles
Dts.TaskResult = ScriptResults.Success
Catch ex As Exception
Dts.Events.FireError(-1, String.Empty, ex.ToString(), String.Empty, 0)
Dts.TaskResult = ScriptResults.Failure
Finally
varCollection.Unlock()
ftpConnection = Nothing
ftpManager = Nothing
End Try
Dts.TaskResult = ScriptResults.Success
End Sub
屏幕截图#1:
屏幕截图#2:
屏幕截图#3:
屏幕截图#4:
屏幕截图#5:
屏幕截图#6:
屏幕截图#7:
屏幕截图#8:
屏幕截图#9:
屏幕截图#10:
屏幕截图#11:
屏幕截图#12:
屏幕截图#13: