Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to search multiple files for one string
#2
I believe something like this may solve your problem!

Code:
Public Function GetFilesContaining(ByVal key As String, ByVal directoryPath As String) As List(Of System.IO.FileInfo)
        Dim filesContaining As New List(Of System.IO.FileInfo) 'This will store what files contain the keyword
        Dim directoryInfo As New System.IO.DirectoryInfo(directoryPath)
        Dim filesInDirectory() As System.IO.FileInfo = directoryInfo.GetFiles("*.txt", True)
        'Now, scroll through all the files and see if it contains a value
        For Each file As System.IO.FileInfo In filesInDirectory
            If (IO.File.ReadAllText(file.FullName).Contains(key)) Then 'The file contains the key
                filesContaining.Add(file) 'Add it to the return list
            End If
        Next

        'Return the list
        Return filesContaining
    End Function

As far as VB.NET help goes, feel free to experiment! Have a goal in mind, and try to accomplish it on your own. That's the best way to learn.
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
Re: How to search multiple files for one string - by brandonio21_phpbb3_import2 - 06-18-2012, 06:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to filter multiple extensions in OPendialog Himansh 1 8,855 05-16-2012, 11:13 PM
Last Post: Himansh
  How to delete all files in a specific folder using VB ? Himansh 4 17,568 03-13-2012, 02:55 PM
Last Post: brandonio21
  Files in a listbox Hurricane 4 19,691 02-27-2012, 03:32 PM
Last Post: brandonio21
  Get Files from the C drive Protection4Real 3 15,460 02-10-2011, 07:59 AM
Last Post: Protection4Real

Forum Jump:


Users browsing this thread: 1 Guest(s)