Best method to parse this flat file

  • Thread starter Thread starter Bill Nguyen
  • Start date Start date
B

Bill Nguyen

I need to parse this flat file programmatically.
A group starts with S record and may contain B C P records
T record is for checksum.

What's the best method to do this. Please give me some sample codes to go
along.

Thanks a million

Bill

-----------------------------------------------------------------------------------------------------


S 1 120405050107 144000144000901 901 148COXT 131445219116E0353
EMPTY - MEMO.
B 110 8592+ 8506+ 75 87.0
C 110 78 1792+ 1774+ 7561.7 87.5F265
C 110 78 1791+ 1772+ 7561.7 87.5G265
C 110 78 3581+ 3544+ 7561.7 87.5B265
C 110 78 942+ 932+ 7661.7 87.5F265
C 110 38 107+ 107+ 6551.5113.0F562
C 110 38 107+ 107+ 6651.5113.0G562
C 110 38 216+ 215+ 6651.5113.0B562
C 110 38 56+ 55+ 7651.5113.0F562
S 1 120834050107 144000144000798 798 148COXT 408445341116W0821
EMPTY - MEMO.
B 110 6595+ 6529+ 74 87.0
C 110 78 1697+ 1680+ 7461.7 87.5F265
C 110 78 3581+ 3545+ 7461.7 87.5B265
C 110 78 942+ 933+ 7461.7 87.5F265
C 110 38 102+ 101+ 7351.5113.0F562
C 110 38 216+ 214+ 7351.5113.0B562
C 110 38 57+ 56+ 7451.5113.0F562
B 111 1499+ 1485+ 73 91.0
C 111 79 1414+ 1401+ 7363.3 90.0A166
C 111 38 85+ 84+ 7151.5113.0A562
T 120835050107 115 1291573
 
So each field seperated by a space will be presentned to you....
this is a snip of code that worked for me.

Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser("C:/testfile.txt")
MyReader.TextFieldType = FileIO.FieldType.Delimited

'====> space as the delimiter
MyReader.SetDelimiters(" ")

Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String = ""
For Each currentField In currentRow
MsgBox(currentField)
If VB.Left(currentField, 1) = "S" Then
''==> do some stuff here <===
End If
If VB.Left(currentField, 5) = "EMPTY" Then
''==> do some stuff here <===
End If

.... ETC. .....

If currentField > "" Then
currentField = counter & currentField & vbCrLf

My.Computer.FileSystem.WriteAllText("C:/testfile.txt", currentField, True,
ascii)
End If
Next
Catch ex As VB.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
'my.Computer.FileSystem.
End Using
 
Thanks Jim! I'll work on it later.

In the meantime, is there a way to use regular expression with this type of
file?

Thanks again

Bill
 
Back
Top