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 txt file

Status
Not open for further replies.

trenta87

Programmer
Oct 29, 2007
13
US
Hi

i need to build a small application which searches through a notepad file and searches for a specific word, once it finds the word i need to extract the next 10 characters after the word. This word appears serveral times in the document.

Any idea how i can do this?
 
Take a look at the System.IO class.

I know this is not the best (there might be a way to read in the whole file at onece and seach, but I have never done that)...but I would open the file, read each line, see if the word exists on the line but finding its position in the line, get the next ten characters and add them to an arraylist, and continue until the end of the file.

Then return the arraylist with all the results found.

If you get stuck on any part, let me know....

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Trenta,

I thought I had something similar to your needs, but mine looks through multiple files in a folder. Anyway, I tried to modify it a little to your needs but its not there yet. You want to change the for loop but this will show you how to read a textfile and parse that data. Hope it helps.


Code:
 Private Sub ReadFile()
        Dim SearchString As String = "" ' Your search word
        Dim FilePath As String = "C:\" ' File location
        Dim FileText, NextTen As String
        Dim Location As Integer

        Dim Dir As System.IO.DirectoryInfo = New DirectoryInfo(FilePath)
        For Each fi As FileInfo In Dir.GetFiles()
            Dim sr As StreamReader = fi.OpenText
            FileText = sr.ReadLine
            Location = InStr(FileText, SearchString)
            NextTen = Mid(FileText, Location + SearchString.Length, 10)
            'Do something with NextTen
            sr.Close()
        Next

    End Sub
 
Or use RegEx:

Code:
Imports System.Text.RegularExpressions
Public Class Form1

	Private WordToFind As String = "Word"
	Private Pattern As String = WordToFind + "(?<=" + WordToFind + ")[\w\s]{10}"
	Private Filename As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\TestFiles\WordToFind.txt"

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim s As String = My.Computer.FileSystem.ReadAllText(Filename)
		TextBox1.Text = s
		For Each m As Match In Regex.Matches(s, Pattern, RegexOptions.IgnoreCase)
			ListBox1.Items.Add(m.ToString.Substring(WordToFind.Length))
		Next

	End Sub
End Class

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top