Grouping Lines from txt file for further processing

vegasit

New member
Joined
Oct 28, 2010
Messages
2
Programming Experience
Beginner
I have a program that opens a fixed length delimited file and process each line depending on what it starts with.

Sample Input:
101 000000000 0000000001010271255A094101National Payment Corp National Payment Corp
5200Happy Company ACH20101021-001.txt PPDDIR DEP 101105101105 1000000000000001
622122400724133133133 000002491300000001 Sam L Adams 0000000000000001
622122400724123123123 000007644900000064 Bravo Bennett 0000000000000002
622122400724144144144 000003310600000009 Nora Elizabeth 0000000000000003
627000000000123232233 000013446800009999 DEBIT Happy Company 0000000000000004
820000000400367202160000001344680000001344681731433493

(attached as well)

I need to read the lines that start with "5" and group them until line "8" basically creating a "batch" for each group.. then have it continue processing the file as needed.

I am trying to use a datatable to do it just because some of these files can be very large and i was told that it would be faster processing.

I have this so far:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim openFileName As String = ""

        openFileName = GetOpenFileName()
        If openFileName.Equals("") Then Exit Sub


        Dim tbl As New DataTable("mytable")
        tbl.Columns.Add("col1", GetType(String))
        'tbl.Columns.Add("col2", GetType(String))
        'tbl.Columns.Add("col3", GetType(Integer))
        Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(openFileName)
        Dim line As String
        Dim aRow As DataRow
        Do
            line = myStream.ReadLine()
            If line Is Nothing Then
                Exit Do
            End If

            Dim sAry As String() = Split(line, vbCrLf) ' separate the fields of the current row         
            aRow = tbl.NewRow 'get a DataRow that has the required structure
            aRow(0) = sAry(0)
            'aRow(1) = sAry(1)
            'aRow(2) = CInt(sAry(2))
            tbl.Rows.Add(aRow)
        Loop
        myStream.Close()
        For Each aRow In tbl.Rows
            Console.WriteLine("{0}", aRow(0))
        Next


    End Sub

Which will output the file to the console just as it is in the text file. but i am not sure to get it to group the lines to do more work on them. The lines that start with "1" and "9" are just processed alone.

Thank you for you help in advance

PS i am very new to programming and this application is my first :)
 

Attachments

  • ACH20101021-001.txt
    3.8 KB · Views: 14
Guh, ACH files. I have a program lying for going through the records for each customer, probably be this afternoon before I can dig it up.
 
Hey that is great!!

I have been working with these for some time now, but the programs that we use don't handle them very well. Any help would be great
 
I slapped something together so you can see taking the flat file and turning it into objects that you can work with. Once you've got all of the information into objects you can do whatever processing you need to on the records.

I couldn't remember anything off of the 8 record and I'm too lazy to go down the hall and get the specs so I omitted it.

I'm using .NET 4.0 here so you won't be able to shorthand setting up the properties since you're using 3.5 but the rest should be the same.

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim groupedRecords = ProcessACHFile("C:\Temp\theFile.ach")

    End Sub

    Private Function ProcessACHFile(ByVal theFilePath As String) As List(Of CustomerRecord)
        Dim customerRecords As New List(Of CustomerRecord)
        Using sr As New IO.StreamReader(theFilePath)
            Dim curLine As String = String.Empty
            Dim curCustomer As CustomerRecord = Nothing
            While sr.Peek <> -1
                curLine = sr.ReadLine
                Select Case curLine.Substring(0, 1)
                    Case Is = 5
                        curCustomer = New CustomerRecord
                        curCustomer.fiveRecord = New FiveRecord(curLine)
                    Case Is = 6
                        curCustomer.sixRecords.Add(New SixRecord(curLine))
                    Case Is = 8
                        curCustomer.eightRecord = New EightRecord(curLine)
                        customerRecords.Add(curCustomer)
                    Case Else
                        'Do Nothing
                End Select
            End While
        End Using
        Return customerRecords
    End Function

End Class

Friend Class FiveRecord
    Public Sub New(ByVal record As String)
        _Name = record.Substring(4, 16).ToString.Trim
        _DiscretionaryData = record.Substring(20, 20).ToString.Trim
        _Identification = record.Substring(40, 10).ToString.Trim
        _EntryClassCode = record.Substring(50, 3).ToString.Trim
        _EntryDescription = record.Substring(53, 10).ToString.Trim
        _EffectiveEntryDate = DateTime.ParseExact(record.Substring(69, 6),
                                                  "yyMMdd",
                                                  System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
    End Sub

    Property Name As String
    Property DiscretionaryData As String
    Property Identification As String
    Property EntryClassCode As String
    Property EntryDescription As String
    Property EffectiveEntryDate As Date
End Class

Friend Class SixRecord
    Public Sub New(ByVal record As String)
        _TotalAmount = CDbl(record.Substring(29, 10)) / 100
        _ReceiverName = record.Substring(54, 22).ToString.Trim
    End Sub

    Property TotalAmount As Double
    Property ReceiverName As String
End Class

Friend Class EightRecord
    Public Sub New(ByVal record As String)
        'Whatever you want to grab on the 8 record
    End Sub
End Class

Friend Class CustomerRecord
    Public Sub New()
        sixRecords = New List(Of SixRecord)
    End Sub

    Property fiveRecord As FiveRecord
    Property sixRecords As List(Of SixRecord)
    Property eightRecord As EightRecord
End Class
 
Back
Top