CVS Text File

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a large text file seperated by commas. I was wondering how to get the
values from the text file. The problem is that not all the characters are
the same length, so I can't use the characters, but is I can't find how to
seperate out the values by the commas. I know that reading the whole line is
possible, but what if I just want the third value and not the rest? Thanks.

exp: 9821,1550,200,2004
 
This should help you

Dim fs As FileStream
Dim sr As StreamReader

Try
Dim line As String
Dim fields As String()
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs)
line = sr.ReadLine()
While Len(line) > 0
fields = Split(line, ",")
For Each field As String In fields
Debug.WriteLine(field)
Next
line = sr.ReadLine()
End While
Catch ex As Exception
' display error
Finally
If Not IsNothing(fs) Then
fs.Close()
sr.Close()
End If
End Try
 
Kou Vang said:
I have a large text file seperated by commas. I was wondering how to get
the
values from the text file. The problem is that not all the characters are
the same length, so I can't use the characters, but is I can't find how to
seperate out the values by the commas. I know that reading the whole line
is
possible, but what if I just want the third value and not the rest?
Thanks.

exp: 9821,1550,200,2004

You can use 'StreamReader' to read the line and split it up into its parts
using 'Split' (specify '","c' as splitting character). Then you can easily
pick the third part from the array returned by 'Split'.

Alternatively you can use the text driver to read a CSV file:

<URL:http://www.connectionstrings.com/>
-> "Text"
 
¤ I have a large text file seperated by commas. I was wondering how to get the
¤ values from the text file. The problem is that not all the characters are
¤ the same length, so I can't use the characters, but is I can't find how to
¤ seperate out the values by the commas. I know that reading the whole line is
¤ possible, but what if I just want the third value and not the rest? Thanks.
¤
¤ exp: 9821,1550,200,2004

You can use ADO.NET and the OLEDB provider to do this:

Dim ConnectionString As String

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "E:\My Documents\TextFiles" & ";" & _
"Extended Properties=""Text;HDR=NO;"""

Dim TextConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
TextConnection.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM CSV#txt", TextConnection)

Dim ds As New DataSet("TextFiles")
da.Fill(ds, "CSV")

Dim dt As DataTable

dt = ds.Tables("CSV")

DataGrid1.SetDataBinding(ds, "CSV")

Dim drCurrent As DataRow
For Each drCurrent In dt.Rows
'Console.WriteLine(drCurrent.Item(0).ToString)
'Console.WriteLine(drCurrent(1).ToString)
Console.WriteLine(drCurrent(2).ToString)
'Console.WriteLine(drCurrent(3).ToString)
'Console.WriteLine(drCurrent(4).ToString)
'Console.WriteLine(drCurrent(5).ToString)
'Console.WriteLine(drCurrent(6).ToString)
Next

TextConnection.Close()


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top