我有一个远程文件夹,我从中选择多个文件并循环访问每个循环容器。但我想先根据该文件夹中的时间戳选择第一个文件。 我如何在SSIS中执行此操作?
答案 0 :(得分:0)
首先你需要创建2个变量
FolderPath (string) -- to store the folder you have to manipulate
dtFiles (Object) -- to store files from this folder
添加脚本任务,选择FolderPath作为ReadOnlyVariable,将dtFiles选为ReadWrite Variable
在脚本中编写以下代码
Imports System.Collections.Generic
Imports System.Linq
Public Sub Main()
Dim strFolderPath As String = Dts.Variables.Item("FolderPath").Value.ToString
Dim lstFiles As New List(Of IO.FileInfo)
For Each strFile As String In IO.Directory.GetFiles(strFolderPath, "*.*", IO.SearchOption.AllDirectories)
Dim fi As New IO.FileInfo(strFile)
lstFiles.Add(fi)
Next
Dts.Variables.Item("dtFiles").Value = lstFiles.OrderBy(Function(x) x.CreationTime).Select(Function(x) x.FullName).ToList
End Sub
将脚本任务连接到The For each loop Container
双击ForEach循环容器并将枚举器类型更改为ADO枚举器,并选择变量dtFiles作为源(在集合选项卡中)并选择枚举模式(第一个表中的行)
在变量映射选项卡中(在For each循环容器中)将索引0映射到一个新变量,即FileName(您可以使用它来完成您的工作)
注意:我使用CreationTime使用了排序文件。您甚至可以使用LastAccessTime和LastWriteTime属性
只需向FolderPath变量和执行
添加值即可