TextParser for a FixedWidth file that has variable lengths

I have library code on this site to parse text files using the .NET TextParser library

http://dyndeveloper.com/ArticleView.aspx?ArticleID=154

and

http://dyndeveloper.com/thread.aspx?Threadid=1378

But today's task is a little different, the file had variable lengths. The code sample below introduces a variable then setting the line lengths to solve the problem

Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic.FileIO
 
Module Module1
    Dim msb As New StringBuilder
 
    Sub Main()
        importFile("W:\Storage\source.txt")
    End Sub
 
    Sub importFile(strFullFileName As String)
        Using sr As New StreamReader(strFullFileName)
            Dim strLine1 As String = sr.ReadLine
            Dim strLine2 As String = ""
 
            While Not strLine1 Is Nothing
                If Left(strLine1, 6) = "System" Then
                    strLine1 = sr.ReadLine
                    Continue While
                End If
                If Left(strLine1, 22) = "          Account Type" Then
                    strLine1 = sr.ReadLine
                    Continue While
                End If
                If Left(strLine1, 7) = "Account" Then
                    strLine1 = sr.ReadLine
                    Continue While
                End If
 
                strLine2 = sr.ReadLine
 
                importFile(strLine1, strLine2)
 
                strLine1 = sr.ReadLine
            End While
        End Using
 
        Dim sw As New StreamWriter("W:\Storage\Export.csv", False)
        sw.Write(msb.ToString)
        sw.Close()
 
    End Sub
    Sub importFile(strLine1 As String, strLine2 As String)
        'these arrays handle the variable formats. This file has three, so we'll need three formats
        Dim intLine1Len As Int16 = strLine1.Length
        Dim aLine1 As Integer() = {32, 37, 29, intLine1Len - 98}
        Dim intLine2Len As Int16 = strLine2.Length
        Dim aLine2 As Integer() = {10, 30, intLine2Len - 40}
 
        'variable to hold an array of the fields in one row
        Dim currentRow As String()
 
        'variable to hold the line in its new format
        Dim strCurrentLine As String = ""
 
 
 
        Try
            '================================================================
            'line 1
            '================================================================
            Dim MyReader As New TextFieldParser(New StringReader(strLine1))
            MyReader.TextFieldType = FileIO.FieldType.FixedWidth
 
            'set the initial format
            MyReader.SetFieldWidths(aLine1)
 
            'read the one line into the currentRow array
            currentRow = MyReader.ReadFields
 
            'for each field in the array, build a new line with the fields separated by commas
            For Each newString In currentRow
                If newString > "" Then
                    If newString.Contains(",") Then
                        strCurrentLine += Chr(34) & newString & Chr(34) & ","
                    Else
                        strCurrentLine += newString & ","
                    End If
                End If
            Next
 
            '================================================================
            'line 2
            '================================================================
            MyReader = New TextFieldParser(New StringReader(strLine2))
            MyReader.TextFieldType = FileIO.FieldType.FixedWidth
 
            'set the initial format
            MyReader.SetFieldWidths(aLine2)
 
            'read the one line into the currentRow array
            currentRow = MyReader.ReadFields
 
            'for each field in the array, build a new line with the fields separated by commas
            For Each newString In currentRow
                If newString > "" Then
                    strCurrentLine += newString & ","
                End If
            Next
 
            'add this new line to our string builder
            msb.AppendLine(strCurrentLine)
            strCurrentLine = ""
 
            MyReader.Close()
 
        Catch ex As Exception
 
            'log the error
 
        End Try
 
    End Sub
End Module

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences