Finding a matching string in multiple .txt files

jmeuse2

Member
Licensed User
Longtime User
I am not very well versed in B4A yet. I would like to use an Input Dialog to enter a string and then search through 100 text files for a matching string. For example; I would like to get a True or False response if "text057.txt" contains a matching string (from the Input Dialog) anywhere in the document (without regard to case). I would appreciate if anyone could help point me in the right direction or share some code examples. Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Consider using a database instead.

You can use something like:
B4X:
For Each f As String In File.ListFiles(File.Combine(File.DirRootExternal, "some_folder"))
   If f.EndsWith(".txt") Then
      Dim s As String = File.ReadString(File.Combine(File.DirRootExternal, "some_folder"), f)
      If s.IndexOf("value to search") > -1 Then
         'MATCHING FILE
      End If
   End If
Next
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
Finding a matching string

Thank you for your quick reply. Your code is far more efficient than the programming punch card idea I had for it.

I filled in the blanks (no doubt incorrectly) and it keeps hanging up on the Line 'For Each f As String In File.ListFiles(File.Combine(File.DirRootExternal, "Keypads"))'. Is there a library I need? Thank you for your assistance Erel!

Sub Search_Click

For Each f As String In File.ListFiles(File.Combine(File.DirRootExternal, "Keypads")) 'Folder name
If f.EndsWith(".txt") Then
Dim s As String = File.ReadString(File.Combine(File.DirRootExternal, "Keypads"), f)
If s.IndexOf("test") > -1 Then
Search.Text = "True" 'MATCHING FILE
End If
End If
Next
End Sub
 
Last edited:
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
Finding a matching string

It works ok now. The error message indicated that the program was pausing on the Line

For Each f As String InFile.ListFiles(File.Combine(File.DirRootExternal, "Keypads"))

This was because I did not have the folder with the file on my cell phone, I errantly placed the folder with the file on the hard drive of my desktop computer. It's what happens when you quit VB6 cold turkey I guess. Thank you for this code.


Jeff
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
testing code tags

codeFor Each f As String InFile.ListFiles(File.Combine(File.DirRootExternal , "Keypads"))/code
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
testing code tags

B4X:
For Each f As String InFile.ListFiles(File.Combine(File.DirRootExternal , "Keypads"))
 
Upvote 0
Top