Hello everybody,
I have a txt file filled with information. The way the txt file is formatted now is each "record" takes up about 10-12 lines of the txt file and there are 20 records.
Example:
Name,Addy,Phone,Age,Sex
SS#,Height,text,text,text
text,text,text,text
Name,Addy,Phone,Age,Sex
SS#,Height,text,text,text
text,text,text,text
Name,Addy,Phone,Age,Sex
SS#,Height,text,text,text
text,text,text,text
Ok so that is what my txt file looks like now. I was able to seperate each field using the comma delimiter.
Example:
Name
Addy
Phone
Age
Sex
Name
Addy
Phone
Age
Sex
Name
Addy
Phone
Age
Sex
Ok now that i have each item on its own line, i want to create a "database", and its in quotes because i don't know if i will use an access db or just use a collection and load the file at runtime, but thats not my problem yet.
My problem is, how can seperate each record.
Below is the code i have so far for seperating the data. If anyone can maybe add to it or explain how i could do this, it would be great appreciated.
Thank you very much for any time and effort.
I have a txt file filled with information. The way the txt file is formatted now is each "record" takes up about 10-12 lines of the txt file and there are 20 records.
Example:
Name,Addy,Phone,Age,Sex
SS#,Height,text,text,text
text,text,text,text
Name,Addy,Phone,Age,Sex
SS#,Height,text,text,text
text,text,text,text
Name,Addy,Phone,Age,Sex
SS#,Height,text,text,text
text,text,text,text
Ok so that is what my txt file looks like now. I was able to seperate each field using the comma delimiter.
Example:
Name
Addy
Phone
Age
Sex
Name
Addy
Phone
Age
Sex
Name
Addy
Phone
Age
Sex
Ok now that i have each item on its own line, i want to create a "database", and its in quotes because i don't know if i will use an access db or just use a collection and load the file at runtime, but thats not my problem yet.
My problem is, how can seperate each record.
Below is the code i have so far for seperating the data. If anyone can maybe add to it or explain how i could do this, it would be great appreciated.
Thank you very much for any time and effort.
VB.NET:
Private Sub importButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles importButton.Click
Dim dataFile As StreamReader
Dim FileName As String
Dim oneLine As String
Dim i As Integer
Try
With OpenFileDialog1
'With statement is used to execute statements using a particular object, here,_
'OpenFileDialog1
.Filter = "Text files (*.txt)|*.txt"
'setting filters so that Comma Seperated Values files choice appears in the Files of Type box
'in the dialog
If .ShowDialog() = DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
FileName = .FileName
dataFile = New StreamReader(.OpenFile)
'using streamreader to read the opened text file
Do While dataFile.Peek() >= 0
oneLine = dataFile.ReadLine()
Dim array() As String
Dim delimStr As String = ",:"""
Dim delimiter As Char() = delimStr.ToCharArray()
array = oneLine.Split(delimiter)
For i = 0 To array.GetUpperBound(0)
If Not Trim(array(i)) = "" Then
If Not array(i).StartsWith("Page") Then ' keyword "Page" is used to determine a new record
ListBox1.Items.Add(Trim(array(i)))
Else
ListBox1.Items.Add("---------------") ' If a new record was found , add the line to show the new record. This was used just for testing purposes and will not be really used
End If
End If
Next
Loop
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (dataFile Is Nothing) Then
dataFile.Close()
End If
End Try
End Sub