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?
There really isn’t a quick and easy way to do this, unfortunately. The basic steps are as follows:
- 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.
- 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
- 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.