Login | Register   
RSS Feed
Download our iPhone app
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



Application Security Testing: An Integral Part of DevOps

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

          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, _
                   Exit Sub
                End If
             End If
             For intarrMessageCount = 0 To arrMessage.Count - 1
                sbMessage.Append(vbCrLf & arrMessage( _
                   intarrMessageCount) & vbCrLf)
             Dim f As New frmShowErrorMessage
             sbMessage.Remove(0, sbMessage.Length - 1)
             MessageBox.Show("No logs for the specified Date", _
                "No Logs", MessageBoxButtons.OK, _
                MessageBoxIcon.Information, _
                MessageBoxDefaultButton.Button1, _
          End If
       Catch ex As Exception
       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 _
         MyReader.TextFieldType = FileIO.FieldType.Delimited
         Dim index As Integer = 0
         While Not MyReader.EndOfData
               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 & _
               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.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date