Splitting a Text File that Has ","
Hi. I have a Text file that looks something like this:
Code:
"157548","asdf","ASDF","F","243","2/19/1993","(H)------","","Teams:","Emergency Contacts: (Uncle)-----(H)9----- (W)----- (C)------,"ENG2D1-05","460977"
So where
"," is, How would I end up splitting them?
I tried something like this:
vb Code:
Public Sub ImportList()
With frmOverview
If .ofdImport.ShowDialog = DialogResult.OK Then
Dim OFDName As String = .ofdImport.FileName
MsgBox(.ofdImport.FileName)
Dim FS As New FileStream(OFDName, FileMode.Open, FileAccess.Read)
Dim SR As New StreamReader(FS)
SR.BaseStream.Seek(0, SeekOrigin.Begin)
While SR.Peek() > -1
Dim ChrArr() As String = {SR.ReadLine}
SR.ReadLine()
For i As Integer = 0 To SR.EndOfStream
Dim ChrArrSplit() As String = ChrArr(i).Split(",")
ChrArr(i) = SR.ReadLine
Next i
End While
End If
End With
End Sub
But it only ended up splitting the CourseCode ("157548")
Re: Splitting a Text File that Has ","
Re: Splitting a Text File that Has ","
ADO.NET is definitely the easiest way to go with a CSV file. For some other delimited or fixed-width formats, or if you don't want to use ADO.NET for some reason, the TextFieldParser class offers the easiest option.
Re: Splitting a Text File that Has ","
vb Code:
Public Sub ImportList()
With frmOverview
If .ofdImport.ShowDialog = DialogResult.OK Then
Dim NewCSV As String = .ofdImport.FileName.Remove(Len(.ofdImport.FileName) - 4, 4)
'File.Copy(.ofdImport.FileName, NewCSV & ".CSV")
SQL = "SELECT * FROM " & .ofdImport.FileName
Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & .ofdImport.FileName & "; Extended Properties=""Text;HDR=Yes;FMT=CSVDelimited""")
Connection.Open()
Command = New OleDbCommand(SQL, Connection)
Adapter.SelectCommand = Command
DS = New DataSet()
Adapter.Fill(DS, "Last Name")
DS.WriteXml("C:\XML.xml")
Connection.Close()
End If
End With
End Sub
I'm having a error occur (I've never used CSV Reading Before, so I'm trying to figure out how to get the File Name from the Selected File & Then Read some Data that is in a Certain Header.
Error:
C:\Users\*\Documents\000000564-Markbook.CSV' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
And it points to: Connection.Open()
So how would I go about opening the File & Then Reading From it - and then say, just Have Last Name Header (Data In THeir) Appear in My Message Box for Testing?
ATM I used a XML Write for the DataSet just to see if would write out the Data in the Header "Last Name"
Re: Splitting a Text File that Has ","
When using ADO.NET to read text files you specify the folder as the data source, not the file. Think of the folder as the database and each file as a table. See www.connectionstrings.com for the specifics.
Re: Splitting a Text File that Has ","
yea but what if there are more than one text file in the folder?
i want to use the text file the user selected.
Re: Splitting a Text File that Has ","
Quote:
Originally Posted by bezaman
yea but what if there are more than one text file in the folder?
i want to use the text file the user selected.
You build your query string only after the user has selected a file. By that time, you will be able to get the folder path and the file name of the selected file. Treat the folder path as the data source and the file name as the table name.