我需要从远程 FTP 站点下载多个 CSV 文件。我正在利用 SSIS,因为这是现场唯一可用的工具。我确实有下载所有文件的 FTP 脚本和一个允许我合并所有文件的 for each 循环。
我想将文件转换为制表符分隔的格式,以避免拆分字段的数据中出现逗号(或者如果有人有其他解决方案,我愿意听)。我有一个可以转换文件的 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."