Results 1 to 7 of 7

Thread: Splitting a Text File that Has ","

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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:
    1. Public Sub ImportList()
    2.         With frmOverview
    3.             If .ofdImport.ShowDialog = DialogResult.OK Then
    4.                 Dim OFDName As String = .ofdImport.FileName
    5.                 MsgBox(.ofdImport.FileName)
    6.                 Dim FS As New FileStream(OFDName, FileMode.Open, FileAccess.Read)
    7.                 Dim SR As New StreamReader(FS)
    8.                 SR.BaseStream.Seek(0, SeekOrigin.Begin)
    9.                 While SR.Peek() > -1
    10.                     Dim ChrArr() As String = {SR.ReadLine}
    11.                     SR.ReadLine()
    12.                     For i As Integer = 0 To SR.EndOfStream
    13.                         Dim ChrArrSplit() As String = ChrArr(i).Split(",")
    14.                         ChrArr(i) = SR.ReadLine
    15.                     Next i
    16.                 End While
    17.             End If
    18.         End With
    19.     End Sub

    But it only ended up splitting the CourseCode ("157548")

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Splitting a Text File that Has ","


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Splitting a Text File that Has ","

    vb Code:
    1. Public Sub ImportList()
    2.         With frmOverview
    3.             If .ofdImport.ShowDialog = DialogResult.OK Then
    4.                 Dim NewCSV As String = .ofdImport.FileName.Remove(Len(.ofdImport.FileName) - 4, 4)
    5.                 'File.Copy(.ofdImport.FileName, NewCSV & ".CSV")
    6.                 SQL = "SELECT * FROM " & .ofdImport.FileName
    7.                 Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & .ofdImport.FileName & "; Extended Properties=""Text;HDR=Yes;FMT=CSVDelimited""")
    8.                 Connection.Open()
    9.                 Command = New OleDbCommand(SQL, Connection)
    10.                 Adapter.SelectCommand = Command
    11.                 DS = New DataSet()
    12.                 Adapter.Fill(DS, "Last Name")
    13.                 DS.WriteXml("C:\XML.xml")
    14.                 Connection.Close()
    15.             End If
    16.         End With
    17.     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"

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    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.

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width