Results 1 to 4 of 4

Thread: Search File In Directory For Text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Search File In Directory For Text

    OK I am trying to get this to work.

    I am looking for a code snippit that searches the files in a specified directory for text that is inputed by the user then adds the files that contain that text to a list box

    any help will be appreciated

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Search File In Directory For Text

    try this:

    vb Code:
    1. For Each file As String In IO.Directory.GetFiles("directory path", "*.txt")
    2.     Dim lines() As String = IO.File.ReadAllLines(file)
    3.     For x As Integer = 0 To lines.GetUpperBound(0)
    4.         If lines(x).Contains(TextBox1.Text) Then
    5.             Debug.Print(file)
    6.         End If
    7.     Next
    8. Next
    9. Debug.Print("done")

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Search File In Directory For Text

    this might be slightly better:

    vb Code:
    1. For Each file As String In IO.Directory.GetFiles("directory path", "*.txt")
    2.     Dim fileText As String = IO.File.ReadAllText(file)
    3.     If fileText.Contains(TextBox1.Text) Then
    4.         Debug.Print(file)
    5.     End If
    6. Next
    7. Debug.Print("done")

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: Search File In Directory For Text

    ok thx i moded this to do what i needed it to do thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width