【问题标题】:How to detect filename dyanamically in SSIS?如何在 SSIS 中动态检测文件名?
【发布时间】:2008-12-24 10:26:35
【问题描述】:

我需要使用 MS SSIS 将平面文件转换为 DB。我需要一种方法来查看特定文件夹以获取(唯一)平面文件,文件名的格式为“FileName-CCYYMMDD.txt”。

如果有办法从文件夹中添加文件,请帮助我 或者 获取格式为“Filename-CCYYMMDD.txt”的文件名,其中 CCYYMMDD 是当前日期,或者根据要求可能是 CurrentDate -1。

任何代码示例或屏幕截图都将受到高度赞赏!

【问题讨论】:

    标签: sql-server-2005 ssis


    【解决方案1】:

    要动态获取连接,您需要在平面文件的 ConnectionString 属性上使用表达式

    例如,要获取“D:\CC080226.txt”,可以使用下面的表达式。

    “D:\CC”
    + RIGHT((DT_WSTR,4)YEAR(GETDATE()),2) + (DT_WSTR,2)MONTH(GETDATE()) + (DT_WSTR,2)DAY(GETDATE()) + ".TXT"

    【讨论】:

      【解决方案2】:

      在数据流中,右键单击平面文件源>显示高级编辑器>组件属性。在自定义属性下,指定 FileNameColumnName 属性的名称。这会将文件路径和名称添加到输出列集合中。

      【讨论】:

        【解决方案3】:

        将文件名放入变量中,可能通过连接,并使用表达式设置变量。

        【讨论】:

          【解决方案4】:

          抱歉,我赶时间,这是脚本的快照,希望对您有所帮助。

          此程序包正在扫描文件夹以查找符合您的规范的文件,然后将控制权传递给数据流。

          alt text http://img395.imageshack.us/img395/8531/dynafilecontrolflowms9.jpg

          控制流,http://img395.imageshack.us/img395/8531/dynafilecontrolflowms9.jpg

          alt text http://img104.imageshack.us/img104/2010/dynafileforeachij5.jpg

          对于每个循环属性,http://img104.imageshack.us/img104/2010/dynafileforeachij5.jpg

          alt text http://img164.imageshack.us/img164/7614/dynafilefilesystemyj1.jpg

          文件系统属性,http://img164.imageshack.us/img164/7614/dynafilefilesystemyj1.jpg

          Imports System
          Imports System.Data
          Imports System.Math
          Imports Microsoft.SqlServer.Dts.Runtime
          Imports System.IO
          
          Public Class ScriptMain
          
              Public Sub Main()
                  Dim Ext As String = ".txt"
                  Dim Path As String = ".\" 'must get from package variable to be more dynamic
                  Dim FileNames() As String = Directory.GetFiles(Path, "CC*" + Ext, SearchOption.TopDirectoryOnly)
                  Dim ValidFileNames As New Collection
                  For Each FileName As String In FileNames
                      Dim FileDate As String = FileName.Substring(Len(Path) + 2) 'first 2 letter is already "CC"
                      If Not IsYmd(FileDate.Substring(0, Len(FileDate) - Len(Ext))) Then Continue For
                      ValidFileNames.Add(FileName)
                  Next
                  Dts.Variables("FileNames").Value = ValidFileNames
                  Dts.TaskResult = Dts.Results.Success
              End Sub
          
          
              Private Function IsYmd(ByVal Test As String) As Boolean
                  If Len(Test) <> 6 Then Return False
                  Dim Year As String = Left(Test, 2)
                  Try
                      If CStr(2000 + CInt(Year)) <> "20" + Year Then Return False
                  Catch ex As Exception
                      Return False
                  End Try
                  Dim Month As String = Mid(Test, 3, 2)
                  Try
                      If CInt(Month) < 1 Then Return False
                      If CInt(Month) > 12 Then Return False
                  Catch ex As Exception
                      Return False
                  End Try
                  Dim Day As String = Right(Test, 2)
                  Dim FirstOfMonth As String = "20" + Year + "/" + Month + "/01"
                  Dim EndOfMonth As Integer = DateAndTime.Day(DateAndTime.DateAdd(DateInterval.Day, -1, _
                      DateAndTime.DateAdd(DateInterval.Month, 1, CDate(FirstOfMonth))))
                  Try
                      If CInt(Day) < 1 Then Return False
                      If CInt(Day) > EndOfMonth Then Return False
                  Catch ex As Exception
                      Return False
                  End Try
                  Return True
              End Function
          
          End Class
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多