Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question: Best Prac text file parsing 1

Status
Not open for further replies.

KCcasey

Programmer
Sep 30, 2003
69
NZ
Hi All,

First off, thanks for taking the time to educate me. This is my first foray into VB.NET, so I really appreciate your constructive feedback.

I have a test file that I want to parse, segregating the first 13(ish) chars of each line from the remaining text in the line.

eg.

[COLOR=red yellow]**.**.**.3080[/color] 250.39
[COLOR=red yellow]11.03.01.3010[/color] 1038.46
[COLOR=red yellow]11.03.01.3090[/color] 9.96

I've tried:

A) parsing the text using substring and streamreader, but I'm can't figure out how to populate the 2nd dimension of the array...
B) using split, again the same problem with populating the 2nd dimension
C) Using an arraylist.

I'd really like some feedback on the typical method of solving a problem like this. Should I be using a multidemensional array, split, arraylist.. a dataset?? And if any of you know of an 'Idiots Guide to Text file parsing' guide anywhere, I'd love the URL :)

Thanks in advance for your feedback guys!

Casey.
 
Could you provide a little bit of clarification on what you mean by "13(ish) chars"? What are the criteria you are using to determine what to extract? Is the record layout fixed width or delimited?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I am not aware of any good resource for text parsing. I have always just worked my own thing as each and every time I have to do this, I find that there is never a standard.

Based on the sample lines you provided, I would look to do something like this:

Code:
Imports System
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim strLine As String = String.Empty
        Dim strParts(1) As String

        ' Open text file
        Using sr As New StreamReader("C:\test.txt")
            ' Loop through file
            Do
                strLine = sr.ReadLine
                ' Separate the parts
                strParts(0) = Trim(strLine.Substring(0, 15))
                strParts(1) = Trim(strLine.Substring(16))
                ' Do whatever elese you need to with these parts
            Loop Until sr.EndOfStream
            ' Remember to close the file you are reading
            sr.Close()
        End Using

    End Sub

End Class

Maybe not the fastest method, but it works and it fairly easy to follow. But it should help you... [smile]

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
I would build a sqlconnection and sqlcommand and execute it...Something like:

Code:
Dim con As New SQLConnection(connectionstring)
Dim com As New SQLCommand(INSERT INTO tablename VALUES (all your values to add)", con)

Try
    con.Open
    com.ExecuteNonQuery
Catch ex As Exception
    ' Handle your errors
Finally
    con.Close
End Try

That should give you a start.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thanks.... I understand that part of it, but how would you include the values of the array to the SQL insert?

Thanks!
 
Based on what we have in previous posts:

Code:
Imports System
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim strLine As String = String.Empty
        Dim strParts(1) As String
        Dim com As New SQLConnection(connectionstring)
        Dim com As New SQLCommand("", con)

        ' Open text file
        Using sr As New StreamReader("C:\test.txt")
            ' Loop through file
            Do
                strLine = sr.ReadLine
                ' Separate the parts
                strParts(0) = Trim(strLine.Substring(0, 15))
                strParts(1) = Trim(strLine.Substring(16))
                ' Note: Single quotes needed around the first value because it is a string
                com.CommandText = "INSERT INTO tablename VALUES ('" & strParts(0) & "', " & strparts(2) & ")"
                Try
                    con.Open
                    com.ExecuteNonQuery
                Catch ex As Exception
                    ' Handle your errors
                Finally
                    con.Close
                End Try
            Loop Until sr.EndOfStream
            ' Remember to close the file you are reading
            sr.Close()
        End Using

    End Sub

End Class

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top