I have a text file that i want to be able to search thru for a word and then find what line it is on and only pull out that line of the text file. How can i do this?
Printable View
I have a text file that i want to be able to search thru for a word and then find what line it is on and only pull out that line of the text file. How can i do this?
Read the text file in line by line and check each line, if you find the word your looking for save the line to a variable in your code.
thats the problem, i never know how big the file will be. it can be 10 lines or 200 lines
The number of lines doesn't matter, you don't need to know this to read in each line.
vb Code:
Imports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'variable to hold the current line Dim strText As String Dim strmReader As New StreamReader("C:\test.txt") 'read in each line and check it for the word Do While strmReader.Peek > -1 strText = strmReader.ReadLine If strText.Contains("word") Then 'add the line of text to a listbox lbText.Items.Add(strText) End If Loop End Sub End Class
This reads each line until the end of the text file is reached, if it finds the 'word' it adds the line to a listbox.
Tinbeard,
Don't forget to close the StreamReader. ;)
Quote:
Originally Posted by stimbo
ahhh good point, scribbled it in a hurry but no excuse for being sloppy :D
You do not have to worry about closing the StreamReader if you enclose it in a Using block. The Using block can be used on anything that implements IDisposable.
ok, thanks, thats exactly what i needed :)
ok, but the way the code works is, that it stops after it finds only the first one on the list. i need to be able to look for a few of them. IE: i was searching for the word "on"
12 on 00:99
18 on 00:88
1 on 00:77
19 on 00:66
5 on 00:87
i wanted to take the one with the highest first number and pull out the last number
show your code.
Ahh well thats a fair bit different from the original question, what have you got so far ?Quote:
Originally Posted by ethanhayon
btw the code I posted will examine every line in the text file and pull out every line with the specified search word.
ok, this is what i need to do..
1) Search text file for a string
2) organize results by highest signal strength (number on line)
3) test to see if the one with highest signal strength matches against a database. If not move on to next line. If it does then i need to return a value of "1"
How can i do this?
1) Loop through the file by line and filter out any line that does not contain your string.
2) Use String.Substring to get the number then cast it to an integer and compare it to the others.
3) If it matches the use Return 1. If it doesn't match then continue the loop.
These are general suggestions as you have not provided a code sample that you have tried. If you provide a sample then I may be able to edit it to make it work properly.
This is my code as posted above
i dont have any of the sql code in yet, but that i know how to do, the problem that i am needing help with is specifically pulling out the field with a higher signal strength and if it doesnt match, i want to be able to continue the search with the next one on the listCode:Dim nwline As String
Dim strmReader As New StreamReader("C:\Users\Ralph\Desktop\bssoutput2.txt")
Do While strmReader.Peek > -1
nwline = strmReader.ReadLine
' MsgBox(nwline)
If nwline.Contains("nw") Then
' MsgBox(nwline)
'''''' Dim strArray() = nwline.Split(" ")
''''Dim mac As String
''''mac = strArray(20)
'MsgBox(nwline)
'TextBox1.Text = (nwline)
' Label1.Text. = (nwline)
ListBox1.Items.Add(nwline)
Dim totalnw = ListBox1.Text
MsgBox(totalnw)
End If
Loop
strmReader.Close()
please, does anybody know how i can do this, i am still stuck on it :(