This is the code I have so far to find all the log files in a folder. But I need to be able to find a specific string in each file, if it is found in one file, stop looking and exit the loop and report back what filename it was in.
There seems to be so many different ways to open a file and search it that I do not know which is the best and I do not typically use VBA but it is all I have access to at the moment.
On a side note, there would be a max of 36 log files and each file max of 5MB each.
Sub StringExistsInFile()
Dim TheString As String
TheString = "MAGIC"
Dim StrFile As String
StrFile = Dir("c:\MyDownloads\*.log")
Do While Len(StrFile) > 0
'Find TheString in the file
'If found, debug.print and exit loop
Loop
End Sub
I had found this code but seems in 2007+ versions of Excel VBA Application.FileSearch was eliminated:
Sub FindText()
'http://www.mrexcel.com/forum/excel-questions/68673-text-file-search-excel-visual-basic-applications.html
Dim i As Integer
'Search criteria
With Application.FileSearch
.LookIn = "c:\MyDownloads" 'path to look in
.FileType = msoFileTypeAllFiles
.SearchSubFolders = False
.TextOrProperty = "*MAGIC*" 'Word to find in this line
.Execute 'start search
'This loop will bring up a message box with the name of
'each file that meets the search criteria
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
End With
End Sub