Search a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ImanMan
    New Member
    • Aug 2007
    • 5

    Search a text file

    hi
    Am using the following code to search a textfile.The returned value will always be found in this case, is there any problem in the code??
    [CODE=vbnet]Dim list As System.Collecti ons.ObjectModel .ReadOnlyCollec tion _
    (Of String)
    list = My.Computer.Fil eSystem.FindInF iles("C:\Find", _
    Search.Text, True, FileIO.SearchOp tion.SearchTopL evelOnly)


    Dim i As Integer = 0

    For Each name As String In list
    RichTextBox1.Te xt = "(" & StringSearch.Te xt & ")" & " " & "Found" & " " & name
    i = 1
    ' StatusText = " Found "
    Next


    If i = 0 Then
    ' StatusText = " Not Found "
    RichTextBox1.Te xt = StringSearch.Te xt & " " & " Not Found"
    End If[/CODE]
    Last edited by Killer42; Aug 7 '07, 03:02 AM. Reason: Added [CODE=vbnet] tag
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by ImanMan
    Am using the following code to search a textfile...
    you can use FileExist to get it. do not use any for loop.
    Last edited by Killer42; Aug 7 '07, 03:03 AM. Reason: Shortened quote block

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by hariharanmca
      you can use FileExist to get it. do not use any for loop.
      Although I'm unfamiliar with the VB.Net syntax, I think the purpose here is to search a bunch of files to find those which contain a given string. I don't believe FileExist will provide that functionality.

      Please correct me if I'm mistaken.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by ImanMan
        [CODE=vbnet]Dim i As Integer = 0
        For Each name As String In list
        RichTextBox1.Te xt = "(" & StringSearch.Te xt & ")" & " " & "Found" & " " & name
        i = 1
        Next
        If i = 0 Then
        RichTextBox1.Te xt = StringSearch.Te xt & " " & " Not Found"
        End If[/CODE]
        I assume the purpose of the above code is to determine whether there are any entries in the list collection. Can't you just check list.Count or something? At the very least, you should Exit For immediately rather than continuing to loop through all entries.

        If this were a VB6 collection, I would expect to code something like[CODE=vb]If list.Count = 0 Then
        RichTextBox1.Te xt = StringSearch.Te xt & " Not Found"
        End If[/CODE]
        Oops! I see, you were trying to list the names of the files which were found. Well, your existing code will show the last name, not all of them. What you need to do is, each time around the For loop, add the name onto the end of RichTextBox1.Te xt, not replace it entirely.

        So, perhaps something like this would make more sense...[CODE=vb]If list.Count = 0 Then
        RichTextBox1.Te xt = StringSearch.Te xt & " Not Found"
        Else
        For Each name As String In list
        RichTextBox1.Te xt = RichTextBox1.Te xt & "(" & StringSearch.Te xt & ")" & " Found " & name & vbNewLine
        Next
        End If[/CODE]

        Comment

        • ImanMan
          New Member
          • Aug 2007
          • 5

          #5
          hello,

          thanks for your reply but, this code is returning for me an always " Found" . What i am trying to do is to search a text file, for example "iman", if he found this string it will return found for me, if not it will return " not found " . Am always getting a found,which is sometimes false
          thanks

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            Originally posted by ImanMan
            hello,

            thanks for your reply but, this code is returning for me an always " Found" . What i am trying to do is to search a text file, for example "iman", if he found this string it will return found for me, if not it will return " not found " . Am always getting a found,which is sometimes false
            thanks
            anyhow, you are using file system object. just run a 'for loop' to search bunch of files with sysObjName.File Exist(<strFileP ath>).

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by hariharanmca
              anyhow, you are using file system object. just run a 'for loop' to search bunch of files with sysObjName.File Exist(<strFileP ath>).
              I assumed that the My.Computer.Fil eSystem.FindInF iles was achieving that.

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Originally posted by Killer42
                I assumed that the My.Computer.Fil eSystem.FindInF iles was achieving that.
                Okay, but i never tryed that. And I don't have VB to try new.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by hariharanmca
                  Okay, but i never tryed that. And I don't have VB to try new.
                  I've never seen or heard of it, either. I was just guessing, based on the name and context.

                  Comment

                  Working...