【问题标题】:how to pick the first file first from specific folder using for each in SSIS [duplicate]如何首先从特定文件夹中使用SSIS中的每个文件选择第一个文件[重复]
【发布时间】:2016-11-07 21:30:14
【问题描述】:

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

【问题讨论】:

  • 你使用的是什么版本?
  • 视觉工作室 2013
  • 远程文件夹是指 FTP 还是仅仅是网络路径? First 是最旧的创建时间戳、最旧的修改时间戳、最旧的访问时间戳吗?

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


【解决方案1】:
  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. 将您的脚本任务连接到 For each 循环容器

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

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

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

只需向 FolderPath 变量添加一个值并执行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多