Wednesday, March 26, 2008

Reading a text file and Extract only the data we want.

This what i tried achieve,
as a part of a project, i created a small VB.NET program to read through outlook mail Inbox and
extract the body of certain emails and create a text file. following is very simplified version of the text file.

eg:---------------------------------------------------
Hi Don,

Name: Don
Last name: Dodempegama
Capital: 1500


Sent: Becky

Kind Regards,

Becky Wang Accounts

-----------------------------------------------------------

from above file I want to extract the values of Name,Last name,Capital and Sent ,

first i open the file to Read,


Dim oFile As System.IO.File
Dim oRead As System.IO.StreamReader
Dim intSingleChar As Integer
Dim cSingleChar As String
Dim s As String
oRead = File.OpenText("C:\test\c.txt")

then read the file one character at at time until the end. during the read I check for ":" in the text file. if I Detect ":" then I extract rest of the line.


While oRead.Peek <> -1
intSingleChar = oRead.Read()
' Convert the integer value into a character
cSingleChar = Chr(intSingleChar)
If cSingleChar = ":" Then
s = oRead.ReadLine.ToString
MsgBox(s)


End If
End While


simple yet effective.