[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Read a VBNet file using a tilde as deliminator instead of a comma

I would like to read a simple tilde deliminated file consisting of ten fields per record. In old fashioned basic we used to be able to specify the deliminator... How do I specify the deliminator  in VBNet?

Thanks
0
GREEBA
Asked:
GREEBA
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
Bob LearnedCommented:
.NET version?

Bob
0
 
GREEBAAuthor Commented:
Bob

.net Professional Version 2002

Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Simple example...

        Dim sr As New System.IO.StreamReader("c:\someFile.txt")
        Dim line As String
        Dim fields() As String
        Dim i As Integer

        line = sr.ReadLine
        While Not (line Is Nothing)
            ' do something with each line in the file
            Debug.WriteLine(line)
            fields = line.Split("~".ToCharArray)

            ' do something with each field of the current line
            For i = 0 To fields.GetUpperBound(0)
                Debug.WriteLine(fields(i))
            Next
            Debug.WriteLine("")

            ' grab the next line in the file
            line = sr.ReadLine
        End While
        sr.Close()
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bob LearnedCommented:
Thanks, Mikey.  I was hoping for the opportunity to show delimited text parsing with 2005.  Too bad, huh?

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...you itching for something to do  today Bob?

Post it anways.  =)

0
 
Bob LearnedCommented:
Yes, sir.  I am struggling with the idiosyncrasies of the RichTextBox control and SelectionTabs, so it is nice to have something else to distract the mind, so that a solution can present itself.

CsvReader class:

Imports Microsoft.VisualBasic.FileIO

Public Class CsvReader

    Public Sub ReadFile(ByVal fileName As String)

        Using reader As New TextFieldParser(fileName)

            reader.TextFieldType = FieldType.Delimited
            reader.Delimiters = New String() {","}

            Dim currentRow As String()

            'Loop through all of the fields in the file.
            'If any lines are corrupt, report an error and continue parsing.
            While Not reader.EndOfData
                Try
                    currentRow = reader.ReadFields()
                    ' Include code here to handle the row.
                Catch ex As MalformedLineException
                    MsgBox("Line " & ex.Message & " is invalid.  Skipping")
                End Try
            End While
        End Using

    End Sub

End Class

Bob
0
 
Bob LearnedCommented:
Minor change for different delimiter:

     reader.Delimiters = New String() {"~"}

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sweet...so many things to discover in .Net.

I myself had not encountered the TextFieldParser() class yet.  =)

Thanx for sharing...
0
 
SanclerCommented:
Or even one form, one button, one datagrid and this code

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = ReadFile("C:\Test", "tildesep.txt")
        DataGrid1.DataSource = dt
    End Sub

    Private Function ReadFile(ByVal sourcepath As String, ByVal sourcefile As String) As DataTable
        Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sourcepath & ";Extended Properties=""text"";"
        Dim SQL As String = "SELECT * FROM " & sourcefile
        Dim Con As New OleDbConnection(ConStr)
        Dim da As New OleDbDataAdapter(SQL, Con)
        Dim dt As New DataTable
        Try
            da.Fill(dt)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return dt
    End Function

With a Schema.ini file, in the same directory as the sourcefile, containing

[tildesep.txt]
Format=Delimited(~)

Done in VB.NET 2003, but translates to 2005 OK.

Roger
0
 
Bob LearnedCommented:
Hellooooo, Roger dodger ;)

Bob
0
 
SanclerCommented:
Hi all.

I see I missed the boat.  Must have been closed whilst I was testing.  ;-)

Roger
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now