Filename = Dir(Filepath & "\" & "*.csv")
While Filename <> ""
SourceFile = Filepath & "\" & Filename
TargetFile = SavePath & "\" & Replace(Filename, ".csv", ".txt")
OpenAsUnicode = False
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
'Detect Unicode Files
Dim Stream: Set Stream = objFSO.OpenTextFile(SourceFile, 1, False)
intChar1 = Asc(Stream.Read(1))
intChar2 = Asc(Stream.Read(1))
Stream.Close
If intChar1 = 255 And intChar2 = 254 Then
OpenAsUnicode = True
End If
'Get script content
Set Stream = objFSO.OpenTextFile(SourceFile, 1, 0, OpenAsUnicode)
arrData = Stream.ReadAll()
Stream.Close
'Create output file
Dim objOut: Set objOut = objFSO.CreateTextFile(TargetFile)
objOut.Write Replace(Replace(arrData,",", "#|#"), Chr(34), "") '-- This line is working fine but it is replacing all the commas inside the text qualifier as well..
objOut.Close
Filename = Dir
Wend
在上面的代码中,行objOut.Write替换(替换(arrData,&#34;,&#34;,&#34;#|#&#34;),Chr(34),&#34; &#34;)正在用#|#替换所有逗号,包括string.so中的逗号。所以我只想替换不是双引号的逗号。
包含字符串的文件 &#34; A&#34;&#34; B,C&#34;,d
我需要的结果是 A#|#B,C#| #D
提前感谢您的帮助。
答案 0 :(得分:0)
如何处理以下内容:
for
基本上,现在objOut.Write Mid(Replace(Replace(arrData,""",""", "#|#"), Chr(34), ""), 2)
为","
进行交换。但是,由于文件以#|#
开头,这还不够。因此,使用"
函数消除了这个。如果文件也以Mid()
结尾,那么您也必须对其进行调整。
根据评论中提到的速度问题,这是我用来测试此解决方案的完整代码:
"
测试文件为350 MB,略超过400万行。代码在不到一分钟的时间内完成。