dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB.NET
Expertise: Intermediate
Nov 4, 2008

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Read a Tab Delimited Text File

This tip shows how to read from a log file using a specified format. The following code takes all the logs for the date mentioned in the date control. The log file is in the follwing format, each value separated by TAB:

Error	FormName	10-06-2006	15:43:12	1	ErrorMessageDescription




Imports Microsoft.VisualBasic.FileIO
Public Shared sbMessage As New System.Text.StringBuilder


#Region "Read from Text File"
    Private Sub btnTextRead_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles btnTextRead.Click
       Dim arrMessage As New ArrayList
       Dim intarrMessageCount As Integer

       Try
          arrMessage = ProcessUsersFile()
          If arrMessage.Count > 0 Then
             If arrMessage.Count = 1 Then
                If InStr(arrMessage(0), "don't exist") Then
                   MessageBox.Show(arrMessage(0).ToString, _
                      "No File", MessageBoxButtons.OK, _
                      MessageBoxIcon.Information, _
                      MessageBoxDefaultButton.Button1, _
                      MessageBoxOptions.DefaultDesktopOnly)
                   Exit Sub
                End If
             End If
             For intarrMessageCount = 0 To arrMessage.Count - 1
                sbMessage.Append(vbCrLf & arrMessage( _
                   intarrMessageCount) & vbCrLf)
             Next
             Dim f As New frmShowErrorMessage
             f.ShowDialog()
             sbMessage.Remove(0, sbMessage.Length - 1)
          Else
             MessageBox.Show("No logs for the specified Date", _
                "No Logs", MessageBoxButtons.OK, _
                MessageBoxIcon.Information, _
                MessageBoxDefaultButton.Button1, _
                MessageBoxOptions.DefaultDesktopOnly)
          End If
       Catch ex As Exception
          MsgBox(ex.Message)
       End Try

    End Sub


   Private Function ProcessUsersFile() As ArrayList
      Dim currentrow(5) As String
      Dim strErrorType, strFormName, strDate, _
         strTime, strThreadId, strErrorMessage As String
      Dim strFileName As String = _
          System.Windows.Forms.Application.StartupPath + _
          "\Error Log.txt"
      Dim boolFileExists As Boolean
      Dim dtEventDate As DateTime
      Dim arrMessage As New ArrayList

      boolFileExists = My.Computer.FileSystem.FileExists(strFileName)
      If boolFileExists = False Then
         arrMessage.Add("File " + strFileName + " don't exist")
         Return arrMessage
         Exit Function
      End If

      Using MyReader As New _
         Microsoft.VisualBasic.FileIO.TextFieldParser(strFileName)
         MyReader.TextFieldType = FileIO.FieldType.Delimited
         MyReader.SetDelimiters(vbTab)
         Dim index As Integer = 0
         While Not MyReader.EndOfData
            Try
               currentrow = MyReader.ReadFields()
               strErrorType = currentrow(0)
               strFormName = currentrow(1)
               strDate = currentrow(2)
               strTime = currentrow(3)
               strThreadId = currentrow(4)
               strErrorMessage = currentrow(5)
               dtEventDate = Convert.ToDateTime(strDate)

               If dtEventDate = dtEventFromDt.Value Then
                  arrMessage.Add(strErrorType & vbCrLf & _
                     strFormName & vbCrLf & strDate & vbCrLf & _
                     strTime & vbCrLf & strThreadId & vbCrLf & _
                     strErrorMessage)
               End If

            Catch ex As Exception
               Throw ex
            End Try

         End While
      End Using
      Return arrMessage
   End Function
#End Region
Arun Thomas
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date