以下脚本的工作原理是将错误记录添加到我的文件的异常中。
我只需要在复制后从源文件中删除坏记录的最佳方法。
Public Sub readopentextfile()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim oFS, oFSO, oFSW
Dim stext As String
Set oFSO = CreateObject("Scripting.FileSystemObject")
'/ Open new DailySalesOrds for reading
Set oFS = oFSO.OpenTextFile("C:\DailySalesOrds.csv", ForReading)
'/ Open exception file for writing
Set oFSW = oFSO.OpenTextFile("C:\DailySalesOrderExcep.csv", ForAppending)
'/ Read each record and count commas if 21 or more add record to exception file
Do Until oFS.AtEndOfStream
stext = oFS.ReadLine
Do
c = InStr(c + 1, stext, ",")
If c <> 0 Then Count = Count + 1
Loop Until c = 0
'Debug.Print Count
If Count >= 21 Then
oFSW.Write (vbNewLine & stext)
End If
c = 0
Count = 0
Loop
End Sub