Parse Tab Delimited Log Files With Visual Basic 2005 'My' Namespace
description Article and source code that shows how to parse a tab delimited log file with Visual Basic 2005.
get resource -> Parse Tab Delimited Log Files With the Visual Basic 2005 'My' Namespace
environment Visual Studio 2005
language Visual Basic
namespace Microsoft.VisualBasic.FileIO.TextFieldParser
tags tab, delimited, parse, textfieldparser, log, log, visual basic, .NET

Fields of data in log text files is often tab delimited. To access the fields of data in a tab delimited log file, or any tab delimited text file for that matter, the file must be parsed. Parsing a tab delimited text file is the process of transforming input text into a data structure, which is suitable for further processing and which captures the fields of data in the input.

Visual Basic 2005 programmers can parse tab delimited files with the TextFieldParser class, a part of the Visual Basic 2005 namespace in Microsoft .NET 2.0.

Note: Some log files are comma delimited. In a previous article I explained how to parse a comma delimited file with the TextFieldParser class. If your log file is comma delimited you can use the example from the previous article to parse it.

The attached source code is a Visual Basic 2005 Windows Application. It parses a tab delimited log file and displays it in a DataGridView:

And excerpt of the source code is shown below.

Code Excerpt

 

Imports Microsoft.VisualBasic

Imports Microsoft.VisualBasic.FileIO

Imports System.Collections.Generic

 

Public Class ExampleOneForm

 

    Private Sub DemoOneForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _

        Handles MyBase.Load

 

        ' Make this form a MDI child of MainForm.

        Me.MdiParent = My.Forms.MainForm

    End Sub

 

    Private Sub loadFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

        Handles loadFileButton.Click

 

        ' Call this form's LoadTabDelimetedTextFileIntoListBox method

        ' to parse a tab delimited file named DemoFile.txt.

        ' DemoFile.txt is located in this project's Bin folder.

        Me.LoadTabDelimetedTextFileIntoListBox("DemoFile.txt")

    End Sub

 

    Private Sub LoadTabDelimetedTextFileIntoListBox(ByVal filePath As String)

 

        ' Declare a variable named theTextFieldParser of type TextFieldParser.

        Dim theTextFieldParser As TextFieldParser

 

        ' Call the My feature's OpenTextFieldParser method passing in a file path.

        ' Assign the resulting TxtFileParser object to theTextFieldParser variable.

        theTextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(filePath)

 

        ' Set TextFieldParser object's TextFieldType property to Delimited.

        theTextFieldParser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited

 

        ' Configure delimiters to handle a tab delimited text file:

        ' Set TextFieldParser object's Delimiter's property to a string array

        ' containing one element with the value ",".

        theTextFieldParser.SetDelimiters(vbTab)

 

        ' Declare a variable named currentRow of type string array.

        Dim currentRow() As String

 

        Try

 

            ' Use the Generic List(Of T) class to create a list to

            '   hold log entries from the .txt file.

            Dim newListOfLogEntry As New List(Of LogEntry)

 

            ' While the end of file has not been reached....

            While Not theTextFieldParser.EndOfData

 

                ' Read the fields on the current line

                ' and assign them to the currentRow array variable.

                currentRow = theTextFieldParser.ReadFields()

 

                ' Create a new LogEntry object from the current row of the text file.

                Dim newLogEntry As New LogEntry(currentRow.GetValue(0).ToString, currentRow.GetValue(2).ToString, currentRow.GetValue(1).ToString)

 

                ' Add the new LogEnry object to the newListOfLogEntry list.

                newListOfLogEntry.Add(newLogEntry)

 

            End While

 

            Me.DataGridView1.DataSource = newListOfLogEntry

 

        Catch malFormLineEx As Microsoft.VisualBasic.FileIO.MalformedLineException

            MessageBox.Show("Line " & malFormLineEx.Message & "is not valid and will be skipped.", "Malformed Line Exception")

        Catch ex As Exception

            MessageBox.Show(ex.Message & " exception has occurred.", "Exception")

        Finally

            ' If successful or if an exception is thrown,

            ' close the TextFieldParser.

            theTextFieldParser.Close()

        End Try

 

    End Sub

 

End Class

 

' LogEntry Class

' Each log entry parsed from the text file

'   and the resulting fields are used to create

'   a LogEntry object is created.

Public Class LogEntry

 

    ' EntryDateTime Property

    '   The time when the log entry

    '   was added to the text file.

    Private m_EntryDateTime As DateTime

    Public Property EntryDateTime() As DateTime

        Get

            Return m_EntryDateTime

        End Get

        Set(ByVal value As DateTime)

            m_EntryDateTime = value

        End Set

    End Property

 

    ' Device Property

    '   The device - server, router, etc.

    '   that generated the log entry.

    Private m_Device As String

    Public Property Device() As String

        Get

            Return m_Device

        End Get

        Set(ByVal value As String)

            m_Device = value

        End Set

    End Property

 

    ' Status Property

    '   The status code of the device

    '   at the time the log entry was

    '   added to the text log.

    Private m_Status As String

 

    Public Property Status() As String

        Get

            Return m_Status

        End Get

        Set(ByVal value As String)

            m_Status = value

        End Set

    End Property

 

    Private Sub New()

        ' Made private to hide it.

        ' Only the following constructor can be used.

    End Sub

 

    ' Constructor.

    Public Sub New(ByVal logEntryDateTime As String, _

                   ByVal device As String, _

                   ByVal status As String)

        Me.EntryDateTime = DateTime.Parse(logEntryDateTime)

        Me.Device = device

        Me.Status = status

    End Sub

 

End Class

Click the link above to download Visual Basic source code in a Visual Studio 2005 solution which demonstrates how to use the TextFieldParser to parse a tab delimited log text file, create LogEntry objects, and display the LogEntry objects in a DataGridView.

For more information visit the link below:

TextFieldParser Class

Get Dot Net Code is a web site full of free and pay for use Visual Basic source code for Visual Basic programmers.

mike mcintyre mvp  http://www.getdotnetcode.com

 

 

 

 

 

 

 

 

 

 



Copyright © 2001-2007 aZ Software Developers. All rights reserved.

 

.