使用SSIS将CSV转换为TAB分隔

时间:2012-06-22 22:59:36

标签: vba csv attributes ssis text-files

我需要从远程FTP站点下载多个CSV文件。我正在利用SSIS,因为这是现场唯一可用的工具。我有FTP脚本下载所有文件,每个循环允许我合并所有文件。

我想将文件转换为TAB分隔格式,以避免数据中的逗号分裂字段(或者如果有人有另一个我愿意听的解决方案)。我有一个VB脚本可以转换文件,但我想利用ActiveX脚本任务或脚本任务等任务在SSIS中运行脚本。如何插入/转换脚本以使用其中一个任务?下面是我用来转换文件的代码。

Dim objFSO, objFile, objFileTSV
Dim strLine, strNewLine, strNewText
Dim FileNameLength, LineLength, NewFileName, Linepos, Quote, QuoteCount, TotalFilesConverted

Set objFSO = CreateObject("scripting.filesystemobject")
strCurPath = objFSO.GetAbsolutePathName(".")
TotalFilesConverted = 0

For Each objFile In objFSO.getfolder(strCurPath).Files
    If UCase(Right(objFile.Name, 4)) = ".CSV" Then
        FileNameLength = Len(objFile.Name)-4
        NewFileName = Left(objFile.Name,FileNameLength) & ".tsv"
        Set objFile = objFSO.OpenTextFile(objFile, 1)

        Do Until objFile.AtEndOfStream
            strLine = objFile.ReadLine
            LineLength = Len(strLine)
            Linepos =1
            strNewLine =""
            Quote = False
            QuoteCount = 0

            Do While Linepos <= LineLength
                If mid(strLine, Linepos, 1) = "," and Not Quote Then 
                    strNewLine = strNewLine + vbTab
                    Quote = False
                Elseif mid(strLine, Linepos, 1) = Chr(34) Then
                    QuoteCount = QuoteCount +1
                    If QuoteCount =2 and Linepos <> LineLength Then
                        If mid(strLine, Linepos, 2) = Chr(34) & Chr(34) Then
                            strNewLine = strNewLine + Chr(34)
                            Linepos = Linepos +1
                            Quote = True
                            QuoteCount = 1
                        Else
                            Quote = False
                            QuoteCount = 0
                        End If
                    Else 
                        Quote = True
                    End If
                Else
                    strNewLine = strNewLine + Mid(strLine, Linepos, 1)
                End If
                Linepos = Linepos +1
            Loop
            strNewText = strNewText & strNewLine & vbCrLF
        Loop
        objFile.Close

        Set objFileTSV = objFSO.CreateTextFile(NewFileName)
        objFileTSV.WriteLine strNewText
        TotalFilesConverted = TotalFilesConverted +1
        strNewText = ""
        objFileTSV.Close

    End If
Next

MsgBox CStr(TotalFilesConverted) + " Files Converted from CSV to TSV."

1 个答案:

答案 0 :(得分:0)

由于SSIS Script Task对象允许您选择C#或VB.NET,因此您可以跟踪有关如何解析CSV文件的大量代码提示(例如,请参阅Parse Delimited CSV in .NET)。

此外,在.NET中循环遍历文件系统非常容易:

For Each dirItem As String In System.IO.Directory.EnumerateFileSystemEntries(DirPath)
    ' Insert code here ...
Next

希望有所帮助!