如何在SSIS

时间:2016-11-07 21:30:14

标签: sql-server ssis sql-server-data-tools bids

我有一个远程文件夹,我从中选择多个文件并循环访问每个循环容器。但我想先根据该文件夹中的时间戳选择第一个文件。 我如何在SSIS中执行此操作?

1 个答案:

答案 0 :(得分:0)

  1. 首先你需要创建2个变量

    FolderPath (string) -- to store the folder you have to manipulate
    
    dtFiles (Object)   -- to store files from this folder
    
  2. 添加脚本任务,选择FolderPath作为ReadOnlyVariable,将dtFiles选为ReadWrite Variable

  3. 在脚本中编写以下代码

     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
    
  4. 将脚本任务连接到The For each loop Container

  5. 双击ForEach循环容器并将枚举器类型更改为ADO枚举器,并选择变量dtFiles作为源(在集合选项卡中)并选择枚举模式(第一个表中的行)

  6. 在变量映射选项卡中(在For each循环容器中)将索引0映射到一个新变量,即FileName(您可以使用它来完成您的工作)

  7. 注意:我使用CreationTime使用了排序文件。您甚至可以使用LastAccessTime和LastWriteTime属性

    只需向FolderPath变量和执行

    添加值即可

    script Task properties

    For Each Loop Enumerator

    For Each Loop variable mapping

    Control Flow preview