读取平面文件的内容并通过验证过程

Read contents of a flat file and go through validation process

我正在尝试使特定类型的文本文件通过数据验证过程的过程自动化。

到目前为止,我已经成功地使用文件系统对象打开了文本文件,并创建了 x 行的计数输出。但是当我使用 EOF 函数检索 x - 1 行时,它不能带单独的行,它认为整个数据是一行。

我无法绕过它对该数据执行进一步的验证检查。关于我如何处理数据集条件验证列表的任何建议。

例如数据集的:

AAA|E0071001|D|20090401010101|EC|UKDC|BP|PARTYID|1|TR01| CPD|AAA123|测试参与者 A123|P|BBB456|新参与者 B456|P| ER2|NAHNAH1|测试参与者|20090401|| EAD|7||| ZZZ|5|1562192240|

以上在文本文件中显示为一行。如果我提取到 excel 或工作它按预期分成 5 行,打破 space 间隙,例如在成为换行符的 CPD 之前。

使用以下:

Do While objTextfile.atendofstream <> True
    objTextfile.skipline
    c = c + 1
    Loop

连同 filesystemsobject,我已经设法计算出#rows = 5。

但如果我执行以下操作:

For i = 1 To (NumLines - 1)
    F.readline
    text = text & F.readline
Next

strLine = F.readline

它只检索一行,不会拆分为 5 行。

我还希望能够分析每个被分隔符破坏的输入,我该怎么做呢?

下面将统计你的txt文档中的行数:

Sub foo()
Dim objFSO
Const ForReading = 1
Dim objTS 'define a TextStream object
Dim fileSpec As String
Dim lineCounter As Long
lineCounter = 0

fileSpec = "C:\Test.txt" 'change the path to whatever yours ought to be
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(fileSpec, ForReading)

Do While Not objTS.AtEndOfStream
    objTS.ReadLine
    lineCounter = lineCounter + 1
Loop

MsgBox lineCounter
End Sub

更新:

要用换行符替换文本文件中的空格,可以执行以下操作:

Sub foo()
Dim objFSO
Const ForReading = 1
Const ForWriting = 2
Dim objTS 'define a TextStream object
Dim strContents As String
Dim fileSpec As String

fileSpec = "C:\Test.txt" 'change the path to whatever yours ought to be
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(fileSpec, ForReading)

'objTS.ReadAll would use read all to read the whole file into memory

Do While Not objTS.AtEndOfStream
    strContents = strContents & " " & objTS.ReadLine 'Read line by line and store all lines in strContents
Loop

strContents = Replace(strContents, " ", vbCrLf)
objTS.Close

Set objTS = objFSO.OpenTextFile(fileSpec, ForWriting)
objTS.Write strContents
objTS.Close
End Sub