Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

Read in comma delimited line value DAT file in VB.NET like you did in VB6

I am looking to read in comma delimited line values in VB.NET The Dat file has various number of lines with carriage return at the end of each line. Each line has a total of 6 values per line. What I am looking to do is read the entire line in and parse the 6 values per line into their respected variables. Below are two examples VB6 and VB.NET.

Example Dat File (6 values per line)
1,2,3,4,5,6
we,are,3,2,1,me
Blue,Green,Red,Purple,Yellow,Black

VB6
Open Trim(sFile) For Input As #1
While Not EOF(1)
    Input #1, stLineValue(0), stLineValue(1), stLineValue(2), stLineValue(3), gGlobalValue, mModuleValue
Wend
Close #1

Open in new window


VB.NET (Something I found)
Dim iFieldNumber As Integer
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(stFileName)
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
			currentRow = MyReader.ReadFields()
			Dim currentField As String
			iFieldNumber = 0
			For Each currentField In currentRow
    			Select Case iFieldNumber
        			Case 0
						stLineValue(0) = currentField
        			Case 1
						stLineValue(1) = currentField
        			Case 2
						stLineValue(2) = currentField
        			Case 3
						stLineValue(3) = currentField
        			Case 4
						gGlobalValue = currentField
        			Case 5
						mModuleValue = currentField
    			End Select
    			iFieldNumber += 1
			Next
	Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
			MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
	End Try
	End While
End Using

Open in new window

0
iamtgo3
Asked:
iamtgo3
  • 2
  • 2
1 Solution
 
käµfm³d 👽Commented:
0
 
iamtgo3Author Commented:
Kaufmed

Thanks for the reply but if you read above that is what I have already posted. In order to get each individual value I had to add a SELECT CASE

			iFieldNumber = 0
			For Each currentField In currentRow
    			Select Case iFieldNumber
        			Case 0
						stLineValue(0) = currentField
        			Case 1
						stLineValue(1) = currentField
        			Case 2
						stLineValue(2) = currentField
        			Case 3
						stLineValue(3) = currentField
        			Case 4
						gGlobalValue = currentField
        			Case 5
						mModuleValue = currentField
    			End Select
    			iFieldNumber += 1
			Next

Open in new window


I was hoping I could read in a line and then take all comma seperated values and assign them to variables without looping through 6 times for every line in the dat file.
0
 
käµfm³d 👽Commented:
My apologies, I didn't inspect that second snippet that closely.

If you know for sure that you will always have 6 fields, then you can ditch the loop and hard-code the field indexes:

e.g.

While Not MyReader.EndOfData
    Try
        currentRow = MyReader.ReadFields()
        stLineValue(0) = currentRow(0)
        stLineValue(1) = currentRow(1)
        stLineValue(2) = currentRow(2)
        stLineValue(3) = currentRow(3)
        gGlobalValue = currentRow(4)
        mModuleValue = currentRow(5)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
    End Try
End While

Open in new window


...but you might encounter maintainability issues if the number of fields change--though to be honest you probably already have those issues.
0
 
iamtgo3Author Commented:
Here it is all put together and I do agree with you on hard coding array size if it should change.

Working example #1
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(stFileName)
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
	
	Dim stLineValue(4) As String
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
			currentRow = MyReader.ReadFields()
			stLineValue(0) = currentRow(0)
			stLineValue(1) = currentRow(1)
			stLineValue(2) = currentRow(2)
			stLineValue(3) = currentRow(3)
			gGlobalValue = currentRow(4)
			mModuleValue = currentRow(5)
		Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
			MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
		End Try
	End While
End Using 

Open in new window


Working example #2
Dim MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(stFileName)
MyReader.Delimiters = New String() {","}
MyReader.HasFieldsEnclosedInQuotes = True
MyReader.TrimWhiteSpace = True
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited

Dim stLineValue(4) As String
Dim currentRow As String()
While Not MyReader.EndOfData
	Try
		currentRow = MyReader.ReadFields()
		stLineValue(0) = currentRow(0)
		stLineValue(1) = currentRow(1)
		stLineValue(2) = currentRow(2)
		stLineValue(3) = currentRow(3)
		gGlobalValue = currentRow(4)
		mModuleValue = currentRow(5)
	Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
		MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
	End Try
End While

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now