Joey Joey - 1 year ago 62 Question

How do I search multiple textfiles for text, then adding that text into a listbox

I've got multiple text files within a folder, like this:


The file names are generated by time and date they were created at so please don't try to open/search the documents using [1-4].txt or something similar as these are just examples.

I would like to search through all of these text files (without knowing their names as they're randomly generated), and if it matches certain text, I would like the rest of the text on that line to be added into a
, then search the next/rest of the text files.

Example of text file contents:

[14:49:16] [Client thread/INFO]: Setting user:
Users Name

All text after
Setting user:
which is on the same line should be added to the ListBox, so in this case,
Users Name
would be added.

The above text will always be the first line of the text file, so no need to search the whole file, the beginning of the text will always be the time created at (which will be different for each text file), then followed by
[Client thread/INFO]: Setting user:
which will always be the same for all of the text files, then
Users Name
, which wont actually output
Users Name
, this is what I would like to find, and then add to the

I've got some of the code created, but there's three problems with it.

1: I have to define the name of the text file, which I will not know.

2: I'm not sure how to search through all of the documents, only the one that is defined.

3: I can get it to output the
Users name
, but only if I remove the leading
[Client thread/INFO]:
, but these items will always be there.

With these three problems, the code is useless, I'm just providing it as possibly it will make it easier for someone to help me?

Public Class Form1
Private Sub LoadFiles()
For Each line As String In IO.File.ReadLines("C:\Example\2016-09-28-1.txt")
'I had to define the name of the text file here, but I need to somehow automatically
'search all .txt files in that folder.

Dim params() As String = Split(line, ": ")

Select Case params(0)
'Text file has to be modified to show as:

Setting user: RandomNameHere

'for the RandomName to show within the ListBox,
'but normally it will never be shown like this within the text files.

Case "Setting user"
End Select
End Sub

Answer Source
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim strFilenames() As String = System.IO.Directory.GetFiles("C:\Example")
    For i As Integer = 0 To strFilenames.GetUpperBound(0)
      Dim strFilename As String = strFilenames(i)
      If System.IO.Path.GetExtension(strFilename).ToLower = ".txt" Then
        For Each strLine As String In System.IO.File.ReadLines(strFilename)
          '[14:49:16] [Client thread/INFO]: Setting user: Users Name
          Dim strSearchText As String = "Setting user: "
          Dim intPos As Integer = strLine.IndexOf(strSearchText)
          If intPos > -1 Then
            Dim strUsername As String = strLine.Substring(intPos + strSearchText.Length)
            MsgBox(strFilename & " - " & strUsername) '<-- replace this with your SELECT CASE or whatever
          End If
        Next strLine
      End If
    Next i
  End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download