我有一个管道分隔文件,需要用CR LF编写

时间:2011-06-22 15:42:22

标签: string visual-studio-2008 vba vbscript

我有一个100MB的文本文件,用管道分隔,带有CR LF记录分隔符。问题是其中一个字段是自由格式文本,并且在字段中包含CRLF。我需要将数据加载到SQL,但当然CRLF会搞砸了。

我用FF FE十六进制值替换了所有CRLF,但现在需要读取文件计数管道分隔符,并在最后一个字段之后插入CRLF。

我必须计算七个管道分隔符,然后最后一个字段是一个24字节的日期字段,我需要插入一个CRLF。

想到最好的方法吗?

2 个答案:

答案 0 :(得分:1)

我没有对此进行测试,但无论如何它可能会让你走上正轨。

Dim b1 As Byte
Dim i As Long

' Open file in Binary mode (since you have at least one binary field)
Open "MyFile.dat" For Binary As #1

Do
    ' Advance to 7th | character from here
    For i = 1 To 7
        Do
            Get #1, , b1
            If b1 = 124 Then ' If Chr(b1) = "|" is perhaps more readable
                ' It's a "|" pipe
                Exit Do
            End If
        Loop
    Next i

    'Skip the 24-byte field
    Seek #1, Seek(1) + 24

    If EOF(1) Then
        ' We're all done
        Exit Do
    End If

    ' Record presumably ends here.
    ' Replace the next two bytes with the CR LF record delimiter
    Put #1, , CByte(10) ' CR
    Put #1, , CByte(13) ' LF
    ' Hopefully those were your hex FF and FE that just got overwritten. 
    ' Should really test for this before overwriting, 
    ' but what the heck.
Loop

Close #1

编辑刚试过它。它非常有效。但是,如果以文本形式阅读,则假设是DOS编码。

答案 1 :(得分:-1)

如果你在变量的内存中有整个东西,你可以使用像myArray = split(var,“|”)这样的分割 然后你可以从0到6连接并在那里添加一个crlf并继续直到字符串的结尾