Solved

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

Posted on 2013-11-25
5
841 Views
Last Modified: 2013-11-26
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
Comment
Question by:iamtgo3
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39675695
0
 
LVL 1

Author Comment

by:iamtgo3
ID: 39675789
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 39675830
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39676500
0
 
LVL 1

Author Comment

by:iamtgo3
ID: 39678663
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now