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!

search for special string in different filetypes

Status
Not open for further replies.

TonyVBnet

Programmer
Mar 31, 2004
17
DE
hey guys.
i've got a problem. my program that i've coded doesn't work. normally it should loop through a folder and its subfolders and find special filetypes. this part works. but i also have a method, which reads a file und tries to find a special word. unfortunately this part doesn't work. i've got no idea why! i've tried a lot, but nothing was successful. the filetypes: *.html, *.htm, *.txt, *.js.

here's my code, perhaps you'll find the mistake.

Code:
Public Function GetFiles(ByVal path As String, ByVal Pattern As String, ByRef arr As ArrayList)
        arr.AddRange(IO.Directory.GetFiles(path, Pattern))
        Dim s As String
        For Each s In IO.Directory.GetDirectories(path)
            GetFiles(s, Pattern, arr)
        Next
        Return arr
    End Function

Public Sub Search()
        Dim sr As IO.StreamReader
        Dim fw As New StreamWriter(_textSearch, True)
        Dim s As String
        Dim r As System.Text.RegularExpressions.Regex
        Dim searchWord As String
        Dim matches As Integer
        For q = 0 To typeArray.Count - 1
            Dim arr As New ArrayList()
            GetFiles(_folder, typeArray.Item(q), arr)
            Dim f As String
            For Each f In arr
                 Try
                    sr = New IO.StreamReader(f)
                    s = sr.ReadToEnd 
                    sr.Close()
                    searchWord = "http"
                    r = New System.Text.RegularExpressions.Regex(searchWord)
                    matches = r.Matches(s).Count
                    If matches > 0 Then
                        fw.WriteLine(f + " - hits: " + matches.ToString)
                    End If
                    s = String.Empty
                Catch ex As Exception
                    fw.WriteLine("following error accured by trying to read the file " & f _
                      & ": " & ex.Message)
                End Try
            Next
            If matches = 0 Then
                fw.WriteLine("nothing found")
            End If
           Next
    End Sub

i hope you'll be able to help me.
thx
 
could try:
Code:
r = New System.Text.RegularExpressions.Regex(searchWord,System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 
thanks for your answer. this also doesn't work.
has anybody else an idea?
 
hi.
i've typed a new codeline:
befor "matches=r.matches(s).count" i've written:
fw.writeline(f)
and it works! but it'll only work, if the output contains the filename (so the "f"). i don't understand it. how is it possible? next problem: he seems to "ignore" the javascript files.
do you have any idea why?
thx
 
thanks for your support.
i asked a collegue and he found the mistake: i forgot to close the streamreader. sometimes it's so easy...
bye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top