如何在SSIS中动态检测文件名?

时间:2008-12-24 10:26:36

标签: sql-server-2005 ssis

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

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

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

4 个答案:

答案 0 :(得分:3)

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

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

“d:\ CC”
+右((DT_WSTR,4)年(GETDATE()),2) +(DT_WSTR,2)MONTH(GETDATE()) +(DT_WSTR,2)DAY(GETDATE()) +“。TXT”

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

抱歉,我很匆忙,这是脚本的快照,希望它有所帮助。

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

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