Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15587

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

$
0
0
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? :ehh:

Viewing all articles
Browse latest Browse all 15587

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>