Solved

# Getting values from a text file line by line

Posted on 2012-03-27
224 Views
I'm currently reading a text file. When I get to a line that contains
"00331," I grab the string following that text then go on to the next line.

Sometimes the lines will look like this...
00111,00445,dewmionw
00386,00331,ewqcweqcqw
00386,02343,ewqcweqcqw

and sometimes like this...
00111,00445,dewmionw
00386,00331,ewqcweqcqw
04386,00331,cwecweqcw
00356,00331,sakcPSKCASO
00111,00445,dewmionw
00386,02343,ewqcweqcqw

In the first example, I grab the value "ewqcweqcqw" and move on...no problem.
In the second example, however, I want to get all three values, then move on.

How do I do this. I've tried several loop variations, but I'm stuck.

TIA
0
Question by:mwmiller78
• 7
• 3
• 3
• +1

LVL 39

Expert Comment

if line.Split(",").Count = 2 first example

else if line.Split(",").Count = 3 ' 2nd example
0

Author Comment

What if there's 4 lines I need? Or 5? Or 20?
0

LVL 39

Expert Comment

what's the overall logic?

you can do a for each:

for each part as String in line.Split(",")

' do something with each part

next
0

LVL 17

Expert Comment

what is the difference between the first and second sets of data to be able to differentiate which set we are looking at?
0

Author Comment

Basically I need to grab the final piece of information any time the line contains "00331"
I'm using this

``````                            strDurationTest = reader.ReadLine()
If strDurationTest.Contains("00331,") Then
strDurationTest = RemoveCoordinates(strDurationTest)
Else
strDurationTest = " "
End If
``````

Which works great in the first set of data. I won't ever know how many instances of "00331" will exist though. I need to be able to do what I have above, look at the next line and do it again if Contains("00221,") = True but move on if not true.
0

LVL 39

Expert Comment

so set a boolean:

dim FirstValueFound as Boolean = false
if not firstValueFound then
If strDurationTest.Contains("00331,") Then
strDurationTest = RemoveCoordinates(strDurationTest)
FirstValueFound = true
Else
strDurationTest = " "
End If
else

if  strDurationTest.Contains("00221,") then
'Do something
end if

firstValueFound = false
end if
0

LVL 83

Expert Comment

Try this
Not sure why you would add an empty item to listbox. If you don't want to do that then move that line to inside the if condition

``````While reader.Peak() >= 0
If strDurationTest.Contains("00331,") Then
strDurationTest = RemoveCoordinates(strDurationTest)
Else
strDurationTest = " "
End If
End While
``````
0

LVL 17

Expert Comment

and what is this new permutation with 00221?
0

Author Comment

Cruiser...
I can't run through the rest of the file. I just need to see if the next line contains the specified value. If it does, repeat. If it does not, move on.
0

Author Comment

Actually if anyone can just tell me if there is a way to preview text in the next line or how to figure out what line I'm on and go back one, I think I'd be set.
0

LVL 17

Accepted Solution

nepaluz earned 500 total points
easiest way is read all lines into a list of string (or array if you are reading a large file) then you can move back and forth (even preview). Obviously, you have your preference for who provides a code suggestion, so I will leave it at that.
0

Author Comment

Here's an example of my text file

00484,2,484,:Satisfaisant/Satisfactory/Zufriedenstellend
00457,00124,Pressureandleaktests
00457,00387,DruckundDichtheitsprÃ¼fung
00448,00117,Water/
00448,00143,Eau/
00448,00162,Wasser
00439,00134,Air/
00439,00147,Air/
00439,00159,Luft
00430,00106,Nitrogen/
00430,00143,Azote/
00430,00168,Stickstoff
00421,00140,Helium
00439,00204,Pressure(gauges)/
00430,00211,Pression/
00430,00247,Druck
00448,00294,Test
00439,00286,duration/
00421,00286,PrÃ¼dauer
00443,00335,Resultconformto/
00425,00356,Ergebnis
00443,00417,AllowableLeakage
00434,00424,/
00425,00419,ZulassigeLeckage
00443,00500,ActualLeakage/
00443,00565,Fuite
00434,00538,Gemessene
00425,00527,Leckage
00404,00020,HydrostaticTest
00404,00106,W
00404,00106,mwmiller78
00404,00106,expertsexchange
00404,00204,2200PSI
00404,00279,2minute
00409,00331,ASMEB16-34/
00400,00331,EN12266-1
00404,00417,NONE
00404,00492,Satisfactory
00377,00020,SeatLeakTest
00377,00106,A
00377,00204,50PSI
00377,00279,N/A
00386,00331,CLASSVIANSI
00377,00331,FCI-70-2-2006/
00368,00331,EN1349
00377,00417,0.15scc/m
00377,00492,0.0scc/m
00356,8,356,Materialtestcertificates
00356,00113,/CertificatsmatiÃ¨res/
00356,00190,BescheinigungenÃ¼berWerkstoffprÃ¼fungen

Here is my code...

``````           Dim AssTestList As New List(Of String)
Dim lineCollection As New Text.StringBuilder()
Dim foundStart As Boolean = False
If line2.Contains("Pressureandleaktests") Then
'Pressure and Leak table exists
foundStart = True
ElseIf line2.Contains("Materialtestcertificates") AndAlso foundStart Then
Exit While
ElseIf foundStart Then
If line2.Contains("00020,") Then

'************************************************************
If strWaterTest.Contains("00106,") Then
strWaterTest = RemoveCoordinates(strWaterTest)
Else
strWaterTest = " "
End If
'************************************************************

intTableNumber = intTableNumber + 1
End If
End If
End While
End Using
``````

This assigns the value "W" to strWaterTest.
I'm trying to get the value of strWaterTest (in this scenario) to be "W mwmiller78 expertsexchange"

In the above example there are three relevant values
W
mwmiller78
expertsexchange

This could be 1, it could be 5, it could be 50. I want to make one string (spaced) with all the  potential values.
0

Author Closing Comment

Ok I finally got it. It's freakin ugly but it does work. If anyone cares, here's the rough draft...

Dim strFisherSpecTemp As String = ""
Dim strFisherSpecTemp2 As String = ""
Dim AssTestList As New List(Of String)
Dim lineCollection As New Text.StringBuilder()
Dim foundStart As Boolean = False
If line2.Contains("Gemessene") Then
foundStart = True
ElseIf line2.Contains("Materialtestcertificates") AndAlso foundStart Then
Exit While
ElseIf foundStart Then
If line2.Contains("00331,") Then
strFisherSpecTemp = line2.Substring(line2.LastIndexOf(",") + 1)
strFisherSpecTemp2 = "00331,"
Do While strFisherSpecTemp2.Contains("00331,") = True
If strFisherSpecTemp2.Contains("00331,") = True Then
strFisherSpecTemp = strFisherSpecTemp & " " & RemoveCoordinates(strFisherSpecTemp2)
End If
Loop
strFisherSpecTemp2 = strFisherSpecTemp2.Replace("00331,", "")
End If
End If
End While
End Using

Thanks for pointing me in the right direction nepaluz!
0

Author Comment

Oops...

Actually added an Exit Do as an Else to the last if statement, otherwise it loops through to EOF effectively voiding the initial While
0

## Featured Post

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.