Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading file lne by line 1

Status
Not open for further replies.

PRUSA

Technical User
Sep 23, 2004
35
US
Hello Everyone,

I trying to get myself familiarized with vb.net.
I've done some vba for excel, acess, and some vbscript.

I have a file with on the 5th line have to parse out a piece of information. How would i got about this this in vb.net

for example file:

Line1:
Line2: abc
Line3:
Line4: abc zyx
Line5: Select "AppName" "DBNAME"

At the line where it begins with Select I need to pull the appname, and passe to a variable. My problem is I saw the readline command but how do I make it go to the next line until it sees the "Select" and then when at the line how do I pull just the appname?

Any help would be appreciated.

Thanks Again,
Sergio
 
I wrote this on the fly, ... hope it has no errors !


dim myLine as string=string.empty
while not sr.peek() ' sr is the stream reader
myLine=sr.readline.toypper()
if myLine.indexof("SELECT")=0 then
dim r() as string= myLine.split(""" """)
' r(1).Substring(0,r(1).length-1) should have "DBNAME" without the quotes
end if
end while

Regards
*** not tested
 
Hi TipGiver,

thanks for getting back to me, i used your code and i get teh following: Index was outside the bounds of the array at the following line:

r(1).Substring(0,r(1).length-1)

please let me know what you think.
 
Hi,
i found a mistake, on the .substring method. (i always do that mistake!)


Dim x As String = "Select ""AppName"" ""DBNAME"""
Dim del As String = """ """ ' It is " " (quote, space, quote)
Dim pos As Integer = x.IndexOf(del) + del.Length
MsgBox(x.Substring(pos, x.Length - 1 - pos))


In the above example, i do a test to see if it works.

- The 'x' is the sr.ReadLine. You see so many db quotes, in order to simulate perfectly what will be read (Select "AppName" "DBNAME").

- The 'Del' is the delimiter (" "). This means that i want to get the text that is after " " (with quotes).

- Th 'Pos' is where i store the position (how many chars) in the 'x' string, that after that the wanted text is located.

- With the 'x.SubString(...)' i get the value DBNAME. The reason that i use the '-1' is to get rid of the last db quote.


Hope this now clears things out!
 
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim file_name As String = "C:\Documents and Settings\Admin\Desktop\Input.txt"
Dim line As String = "", app_name As String = ""

FileOpen(1, file_name, OpenMode.Input)
Do While Not EOF(1)
line = LineInput(1)

If InStr(UCase(line), "SELECT") = 1 Then
line = line.Replace(Chr(34), "")
Dim arr() As String = Split(line, " ")
app_name = arr(1)
Debug.Print(app_name)
End If
Loop
FileClose(1)
End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top