views:

2890

answers:

5

Hi,

I need to convert a flat file to DB using MS SSIS. I need a way to look into a particular folder to fetch the (only) flat file, filename is of the format "FileName-CCYYMMDD.txt".

Please help me if there is way to add a file from the folder OR Get a file name of the format "Filename-CCYYMMDD.txt" where is CCYYMMDD is the current date or maybe CurrentDate -1 according to requirements.

Any code examples or screenshots will be highly appreciated!

A: 

Put the filename in a variable, possibly via a connection, and set up the variable with an expression.

ConcernedOfTunbridgeWells
+1  A: 

To get the connection dynamically, you need to use expression on the ConnectionString property of the flat file

Example, to get "D:\CC080226.txt", you can use the below expression.

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

Ken Yao
A: 

Sorry, am in a hurry, here is the snapshots n the scripts, hopefully it helps.

This package is scanning a folder for files conforming your specs, then pass the control to a Data Flow.

Control Flow, http://img395.imageshack.us/img395/8531/dynafilecontrolflowms9.jpg

For Each Loop Properties, http://img104.imageshack.us/img104/2010/dynafileforeachij5.jpg

File System Properties, 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
Irawan Soetomo
A: 

In the data flow, right click on the Flat File Source>Show advanced editor>Component Properties. Under Custom Properties, specify a name for the FileNameColumnName property. This will add the file path and name to the output columns collection.

Will