devxlogo

Searching Multiple Files

Searching Multiple Files

Question:
I use VB5 and I’m trying to search through multiple files for a string and open this file in a TextBox. How can I do this?

Answer:
There really isn’t a quick and easy way to do this, unfortunately. The basic steps are as follows:

  1. Create a list of the files to be searched. You can use the Dir function to return a list of files in a particular directory.
  2. Open each file in order and look for the piece of text in the file. The code to do this is fairly simple:
       Dim sBuffer As String   Const sSearchString = “REM”      Open “C:Autoexec.bat” For Input As #1   sBuffer = Input$(LOF(1), 1)   Close #1      If InStr(1, sBuffer, sSearchString, vbTextCompare) > 0 Then      MsgBox “Found string in file”   End If
  3. The InStr function returns the position at which the string you wanted was found in the buffer. You can then put it into a TextBox control.

devx-admin

Share the Post: