我有一个名为'Data'的文件夹,其中包含文件名中包含当前日期的所有文件,例如'myfile 20-08-2011'.现在我想创建一个SSIS包,它收集08月份的所有文件,也就是说,我想逐月整理文件并将这些文件复制到名为"august"的新文件夹中.我怎样才能做到这一点?
小智 31
这是一个可能的解决方案,以帮助实现这一目标Foreach loop container,Script Task和File System Task.您可以在没有文件系统任务的情况下执 但是,我使用它来利用内置控制流任务来移动文件.该示例是使用创建的SSIS 2005.
该示例假定文件将被统一命名.因此,该示例使用格式File DD-MM-YYYY.例如,文件将被命名File 29-07-2011,File 15-08-2011等等.
在SSIS包上,创建以下变量.在此示例中,源文件存储在文件夹位置F:\Temp\,文件应移动到位置*F:\ Temp\Monthwise*.在目标文件夹中,每个月都会有文件夹,如7月,8月等.
DestinationFolder变量将保存最终目标文件夹值,F:\Temp\Monthwise\August但此变量将分配脚本任务中的实际值.现在,让我们分配值F:\Temp\Monthwise\.此临时值是为了避免文件系统任务在设计时抛出错误消息.
DestinationRoot将包含实际的根文件夹,在该文件夹下,应根据月份名称创建7月,8月等文件夹.
SourceFolder表示最初存储所有文件的文件夹.在此示例中,源文件夹将是F:\Temp\
SourceFilePath表示实际的文件路径.当Foreach循环容器循环遍历每个变量时,将为该变量分配单个文件值.为避免文件系统任务在设计时抛出错误消息,让我们为它分配一些虚拟值F:\Temp\1.txt.
FilePattern定义应在给定源文件夹路径中循环的文件模式.让我们分配*.*,这意味着将循环所有文件.您还可以指定*.txt或File*.txt或My*.xls等,这是高达您的要求.
MonthStartPosition表示月份值在文件名中开始的位置.因此,在文件名格式中File 29-07-2011,月份07从第9个字符开始.因此价值9.
MonthLength指定要提取的字符数.无论如何这将是2个字符,但我不想硬编码.所以,我创建了一个变量.
MonthNameFormat指定应如何创建文件夹.值MMMM表示它将创建具有完整月份名称的文件夹,如1月,2月等.如果我们使用值MMM,则文件夹将创建为Jan,Feb等.仅当文件夹不存在时才会创建文件夹.

在SSIS包的" 控制流"选项卡上,放置Foreach loop container并配置它以SourceFolder使用文件模式变量循环遍历变量中指定的文件夹FilePattern.当Foreach循环容器循环遍历文件时,文件名将分配给变量SourceFilePath.我们将使用此变量来获取Script Task中的月份值.



在Foreach循环容器中,放置一个Script Task和脚本任务的Script部分,单击Design script ...按钮打开VSTA编辑器并粘贴这些屏幕截图后提供的代码.由于该示例是在VS 2005中创建的,因此代码是用VB.NET编写的,因为这是唯一受支持的语言SSIS 2005.



脚本任务代码:代码从变量中获取完整的文件路径值,SourceFilePath并仅提取文件名以将其存储在局部变量中FileName.
然后检查是否为MonthStartPosition和MonthLength变量分配了正确的非零值.然后它提取月份值以将其存储在局部变量中MonthValue.
Uisng MonthValue,它使用DateTime函数获取完整的月份名称值.值1分配给日期和年份,因为我们只需要月份名称.
局部变量FolderName中的月份名称与DestinationRoot值组合以检查文件夹是否存在.如果该文件夹不存在,将创建该文件夹,以便文件系统任务不会失败.
最后,将完整的目标文件夹值分配给包变量DestinationFolder.此变量将在文件系统任务中使用.
VB.NET code for SSIS 2005
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Public Class ScriptMain
Public Sub Main()
Dim varCollection As Variables = Nothing
Dts.VariableDispenser.LockForRead("User::SourceFilePath")
Dts.VariableDispenser.LockForRead("User::DestinationRoot")
Dts.VariableDispenser.LockForRead("User::MonthStartPosition")
Dts.VariableDispenser.LockForRead("User::MonthLength")
Dts.VariableDispenser.LockForRead("User::MonthNameFormat")
Dts.VariableDispenser.LockForWrite("User::DestinationFolder")
Dts.VariableDispenser.GetVariables(varCollection)
Dim SourceFilePath As String = varCollection("User::SourceFilePath").Value.ToString()
Dim FileName As String = SourceFilePath.Substring(SourceFilePath.LastIndexOf("\") + 1)
Dim DestinationRoot As String = varCollection("User::DestinationRoot").Value.ToString()
Dim MonthStartPosition As Integer = Convert.ToInt32(varCollection("User::MonthStartPosition").Value)
Dim MonthLength As Integer = Convert.ToInt32(varCollection("User::MonthLength").Value)
Dim MonthValue As Integer = 0
Dim MonthNameFormat As String = varCollection("User::MonthNameFormat").Value.ToString()
Dim FolderName As String = String.Empty
Dim MonthwiseDirectory As String = String.Empty
If MonthStartPosition > 0 AndAlso MonthLength > 0 Then
MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength))
End If
If FileName.Length > 0 AndAlso MonthValue > 0 Then
FolderName = New DateTime(1, MonthValue, 1).ToString(MonthNameFormat)
End If
MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName)
If Not System.IO.Directory.Exists(MonthwiseDirectory) Then
System.IO.Directory.CreateDirectory(MonthwiseDirectory)
End If
varCollection("User::DestinationFolder").Value = MonthwiseDirectory
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
C# code for SSIS 2008 and above
public void Main()
{
Variables varCollection = null;
Dts.VariableDispenser.LockForRead("User::SourceFilePath");
Dts.VariableDispenser.LockForRead("User::DestinationRoot");
Dts.VariableDispenser.LockForRead("User::MonthStartPosition");
Dts.VariableDispenser.LockForRead("User::MonthLength");
Dts.VariableDispenser.LockForRead("User::MonthNameFormat");
Dts.VariableDispenser.LockForWrite("User::DestinationFolder");
Dts.VariableDispenser.GetVariables(ref varCollection);
string SourceFilePath = varCollection["User::SourceFilePath"].Value.ToString();
string FileName = SourceFilePath.Substring(SourceFilePath.LastIndexOf('\\') + 1);
string DestinationRoot = varCollection["User::DestinationRoot"].Value.ToString();
int MonthStartPosition = Convert.ToInt32(varCollection["User::MonthStartPosition"].Value);
int MonthLength = Convert.ToInt32(varCollection["User::MonthLength"].Value);
int MonthValue = 0;
string MonthNameFormat = varCollection["User::MonthNameFormat"].Value.ToString();
string FolderName = string.Empty;
string MonthwiseDirectory = string.Empty;
if (MonthStartPosition > 0 && MonthLength > 0)
{
MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength));
}
if (FileName.Length > 0 && MonthValue > 0)
{
FolderName = new DateTime(1, MonthValue, 1).ToString(MonthNameFormat);
}
MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName);
if (!System.IO.Directory.Exists(MonthwiseDirectory))
{
System.IO.Directory.CreateDirectory(MonthwiseDirectory);
}
varCollection["User::DestinationFolder"].Value = MonthwiseDirectory;
Dts.TaskResult = (int)ScriptResults.Success;
}
Run Code Online (Sandbox Code Playgroud)
在Foreach循环容器中,在"脚本"任务之后放置" 文件系统任务".配置文件系统任务,如屏幕截图所示.

配置包任务后,"控制流"选项卡应如下所示.

我们来测试一下这个包.在此之前,源文件夹F:\ Temp的内容如下所示.文件是假的.因此,大小为0 KB.

下面的屏幕截图显示了包的成功执行.

下面的屏幕截图显示了如何将文件移动到基于月份名称创建的相应目标文件夹.各个文件夹的内容如下所示.
希望有所帮助.





| 归档时间: |
|
| 查看次数: |
54964 次 |
| 最近记录: |