Tuesday, September 4, 2012

SSIS source file multiple formats

if the source file formats are changing intermittently , this mostly happens when the file is copied from a unix or macintosh to a windows system through FTP. if the FTP program is using an binary option then it wont change , but if you still facing the problem with different formats with the line feed charecters as below

crlf - windows
lf - unix
cr - macintosh

so when you devlop a package using lf as a row delimitor then your package might fail or give some problem when the formats got changed.

below are the steps to convert all the format types in all the files to a one specific format.

if you are looping through the files using "for each loop container"

add a script task before the dataflow and add this code

Note : below component will change all the charecters (line feed) in the text file with the new one , so make sure if you are importing any text file which has these charecters inside text.

Imports System.IO -- keep this at the first line

  ' Add your code here

     Dim file As New System.IO.StreamReader(Dts.Variables("User::vFileName").Value.ToString())
        Dim data As String
        data = file.ReadToEnd()
        data = data.Replace(vbCrLf, vbLf)
        data = data.Replace(vbCr, vbLf)
        'data = data.Replace(vbLf, vbCrLf)
        file.Close()

        Dim writer As New System.IO.StreamWriter(Dts.Variables("User::vFileName").Value.ToString(), False)
        writer.Write(data)
        writer.Flush()
        writer.Close()

        Dts.TaskResult = ScriptResults.Success

        '
        Dts.TaskResult = ScriptResults.Success

No comments:

Post a Comment

Featured Post

SQL Server AWS Migration BCP

stored procedure to generate BCP scritps to migrate the SQL Server database. Developed this stored procedure on my labs to simulate t...

Contributors