Learn AspNet
Learn AspNet

Reputation: 1612

Read complex text file using Vb.net

File Structure

RECORD
1234567890,123456789,1234567,Address1,,City,State,Zip,USA,Phone,,,,,,,,,
EmpName,DBAName,ID,,Address1,,Address1,City,60603,USA,234567890,,,,
C,03/13/2017,,1,2,
RECORD
1234567890,123456789,1234567,Address1,,City,State,Zip,USA,Phone,,,,,,,,,
EmpName2,DBAName2,ID2,,Address2,,Address2,City2,60603,USA,234567890,,,,
C,03/13/2017,,1,2,

Looking at the above file structure, I want to loop through file and for each record(RECORD), I want to put next three lines in one array and then next record in separate array.

I am looking for skeleton code, I am very new in programming using stream readers.

Code so far

 Dim read As New System.IO.StreamReader("C:\New Text.txt")
       Dim a As String

       For Each a In read.ReadLine
           If read.ReadLine.Equals("RECORD") Then
               \\How do I read next 3 lines and put them in one array with comma delimiter
           End If
       Next

Upvotes: 0

Views: 348

Answers (2)

Steve
Steve

Reputation: 216293

You need a data structure where you will keep the lines loaded and then start your loop

' Here you will keep the 3 lines block joined together and splitted on comma
Dim records = new List(Of String())()

Using read As New System.IO.StreamReader("C:\New Text.txt")
   Dim a As String

   ' Read each line (Assuming that we start with a RECORD line
   For Each a In read.ReadLine
       ' Do not read another line, but just test what is present in the string
       If a.Equals("RECORD") Then

          ' Now read the 3 following lines....
          Dim line1 = read.ReadLine
          Dim line2 = read.ReadLine
          Dim line3 = read.ReadLine
          'Join the lines togeter
          Dim record = string.Join("", line1, line2, line3).
          ' Split on the comma to produce an array of strings. The fields.
          Dim fields = record.Split(","c)
          records.Add(fields)
       End If
   Next
End Using

Of course this should be enhanced using a proper class that describe your inputs where each property of the class represent a field of the CSV file loaded. And then you could change the List(Of String()) to a List(Of YourClassData)

Keep in mind that this solution is extremely dependant of an exact file structure. A line with the "RECORD" content should be always followed by three lines of data. (No blank lines allowed between the three data lines)

Upvotes: 4

Trey
Trey

Reputation: 413

     Dim read As New System.IO.StreamReader("C:\New Text.txt")
//I usually only do C#, but rough VB.NEt code: I did not rcreate your array for you, figured you got that :-)
       Dim a As String
       Dim myConcatString as String
       For Each a In read.ReadLine
          Dim myReadLine as String 
            myReadLine = read.ReadLine
           If myReadLine.Equals("RECORD") Then
myConcatString = myConcatString  & myReadLine 
               \\How do I read next 3 lines and put them in one array with comma delimiter
           else
           //add myConcatString to array if not null....
myConcatString =""
           End If
       Next

Upvotes: -1

Related Questions