Imports System.IO
Public Class Form1
Private FormattedAccountDetails As New List(Of AccountRecord)
Dim ConversionFailure As New Boolean
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim NewAccountRecognised As Boolean
Dim NewDetailRecordsStarted As New Boolean
Dim MyHeaderRecord As New AccountRecord
'add your own files back to these read and write streams
Dim srReader As New StreamReader("d:\temp\InputFile.txt")
For Each strBuffer As String In srReader.ReadToEnd.Split(Environment.NewLine)
Dim strBufferElements() As String = strBuffer.Trim.Split({" "}, StringSplitOptions.RemoveEmptyEntries)
If strBufferElements.Count > 0 Then
If strBufferElements(0).Trim.StartsWith("Given") Then
'New account is recognised so the next read of the file is the actual details
NewAccountRecognised = True
ElseIf NewAccountRecognised Then
'This is the new account details section
MyHeaderRecord = CreateHeaderAccount(strBufferElements)
NewAccountRecognised = False
NewDetailRecordsStarted = False
ElseIf strBufferElements.Count = 2 Then
'this has to be the header dates on a new line
MyHeaderRecord = AddDatesToHeaderAccount(MyHeaderRecord, strBufferElements)
ElseIf strBufferElements(0).Trim.StartsWith("Test") Then
'this is the start of the detail records for each given header account record
NewDetailRecordsStarted = True
ElseIf strBufferElements.Count = 1 Then
'ignore this element since the is the start of the file
ElseIf NewDetailRecordsStarted Then
'this is a detail record for each given header account record
Dim DetailRec As New AccountRecord
DetailRec = MyHeaderRecord.Clone
DetailRec = AddAccountDetails(DetailRec, strBufferElements)
FormattedAccountDetails.Add(DetailRec)
Else
MsgBox("Error In File Conversion - Check Input File Structure!")
ConversionFailure = True
Exit For
End If
End If
Next
If Not ConversionFailure Then
Dim swWriter As New StreamWriter("d:\temp\OutPutFile.txt", False)
Dim HR As AccountRecord = CreateHeaderRecordForFile()
swWriter.WriteLine(String.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}", HR.AccountNo, HR.FundID, HR.TransationType, HR.RecordDate, HR.ReinvestDate, HR.TestID, HR.Balance, HR.BalanceRatio, HR.Units))
For Each AR As AccountRecord In FormattedAccountDetails
'to show an example of the output add a textbox with multiline true and uncomment the code below
'TextBox1.Text += String.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}", AR.AccountNo, AR.FundID, AR.TransationType, AR.RecordDate, AR.ReinvestDate, AR.TestID, AR.Balance, AR.BalanceRatio, AR.Units) & vbCrLf
swWriter.WriteLine(String.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}", AR.AccountNo, AR.FundID, AR.TransationType, AR.RecordDate, AR.ReinvestDate, AR.TestID, AR.Balance, AR.BalanceRatio, AR.Units))
Next
swWriter.Close()
MsgBox("File Conversion Completed Successfully!")
End If
End Sub
Private Function CreateHeaderAccount(ByVal strAccountDetails() As String) As AccountRecord
Dim NewAccountRecord As New AccountRecord
Select Case strAccountDetails.Count
Case 3
With NewAccountRecord
.AccountNo = strAccountDetails(0).Trim
.FundID = strAccountDetails(1).Trim
.TransationType = strAccountDetails(2).Trim
End With
Case 5
With NewAccountRecord
.AccountNo = strAccountDetails(0).Trim
.FundID = strAccountDetails(1).Trim
.TransationType = strAccountDetails(2).Trim
.RecordDate = strAccountDetails(3).Trim
.ReinvestDate = strAccountDetails(4).Trim
End With
Case Else
MsgBox("Error In File Conversion - Check Input File Structure!")
ConversionFailure = True
End Select
Return NewAccountRecord
End Function
Private Function AddDatesToHeaderAccount(ByVal AccountHeader As AccountRecord, ByVal strAccountDetails() As String) As AccountRecord
Select Case strAccountDetails.Count
Case 2
With AccountHeader
.RecordDate = strAccountDetails(0).Trim
.ReinvestDate = strAccountDetails(1).Trim
End With
Case Else
MsgBox("Error In File Conversion - Check Input File Structure!")
ConversionFailure = True
End Select
Return AccountHeader
End Function
Private Function AddAccountDetails(ByVal MyDetailRecord As AccountRecord, strAccountDetails() As String) As AccountRecord
Select Case strAccountDetails.Count
Case 4
With MyDetailRecord
.TestID = strAccountDetails(0).Trim
.Balance = strAccountDetails(1).Trim
.BalanceRatio = strAccountDetails(2).Trim
.Units = strAccountDetails(3).Trim
End With
Case Else
MsgBox("Error In File Conversion - Check Input File Structure!")
ConversionFailure = True
End Select
Return MyDetailRecord
End Function
Private Function CreateHeaderRecordForFile() As AccountRecord
Dim HF As New AccountRecord
With HF
.AccountNo = "Given Account #"
.FundID = "Fund ID"
.TransationType = "Transaction"
.RecordDate = "Record Date"
.ReinvestDate = "Reinvest Date"
.TestID = "TestID"
.Balance = "Balance"
.BalanceRatio = "Balance Ratio"
.Units = "Units"
End With
Return HF
End Function
Private Class AccountRecord
Public Property AccountNo As String
Public Property FundID As String
Public Property TransationType As String
Public Property RecordDate As String
Public Property ReinvestDate As String
Public Property TestID As String
Public Property Balance As String
Public Property BalanceRatio As String
Public Property Units As String
Public Function Clone() As AccountRecord
Return DirectCast(Me.MemberwiseClone(), AccountRecord)
End Function
End Class
End Class