Results 1 to 5 of 5

Thread: Reading only one field from CSV file using TextFieldParser.ReadFields Method

Threaded View

  1. #1

    Thread Starter
    New Member james1628's Avatar
    Join Date
    Sep 2020
    Posts
    5

    Reading only one field from CSV file using TextFieldParser.ReadFields Method

    I am trying to read a particular field of a CSV file using ReadFields Method of TextFieldParser class. I will use the example at Microsoft to to show what I want to do.

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser.readfields?view=netcore-3.1#Microsoft_VisualBasic_FileIO_TextFieldParser_ReadFields

    Code:
    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
        MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        MyReader.Delimiters = New String() {","}
        Dim currentRow As String()
        
        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()
        
                'How to read only one field instead of a row in this section?
    
                For Each currentField As String In currentRow
                    My.Computer.FileSystem.WriteAllText(
                        "C://testfile.txt", currentField, True)
                Next
             Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message & " is invalid.  Skipping")
            End Try
        End While
    End Using
    
    From Microsoft's example, how do I get to retrieve, for example, only the second field of a 3-fields CSV file?
    Last edited by james1628; Sep 16th, 2020 at 08:01 PM. Reason: improve title

Tags for this Thread

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