![]() |
Search a text file for a string
I want to search a given text file for a certain string. Is this possible?
If so, how would I code it? The file path and name is stored in the variable Filename. The search text is "NODE DATA" I just want excel to search the file given and return a boolean (True/False) as to whether the string exists in the text file. All help is appreciated. Thanks. |
Search a text file for a string
As usual, it's not until after you post your question that you stumble onto
the solution... With Application.FileSearch .NewSearch .LookIn = Filename .SearchSubFolders = True .TextOrProperty = "NODE DATA" .FileType = msoFileTypeAllFiles If .Execute 0 Then ...some code End if End With "crazybass2" wrote: I want to search a given text file for a certain string. Is this possible? If so, how would I code it? The file path and name is stored in the variable Filename. The search text is "NODE DATA" I just want excel to search the file given and return a boolean (True/False) as to whether the string exists in the text file. All help is appreciated. Thanks. |
Search a text file for a string
Maybe this is faster and it doesn't need an extra reference:
Function CheckForStringInFile(strString As String, _ strFile As String) As Boolean 'result will be True if string is in the file 'and False if the string is missing 'because of vbBinaryCompare this will be case sensitive '------------------------------------------------------ Dim hFile As Long Dim buff As String On Error GoTo ERROROUT 'obtain file handle, open file 'and load into a string buffer hFile = FreeFile Open strFile For Input As #hFile buff = Input$(LOF(hFile), hFile) Close #hFile If InStr(1, buff, strString, vbBinaryCompare) = 0 Then CheckForStringInFile = False Else CheckForStringInFile = True End If Exit Function ERROROUT: On Error GoTo 0 End Function RBS "crazybass2" wrote in message ... As usual, it's not until after you post your question that you stumble onto the solution... With Application.FileSearch .NewSearch .LookIn = Filename .SearchSubFolders = True .TextOrProperty = "NODE DATA" .FileType = msoFileTypeAllFiles If .Execute 0 Then ...some code End if End With "crazybass2" wrote: I want to search a given text file for a certain string. Is this possible? If so, how would I code it? The file path and name is stored in the variable Filename. The search text is "NODE DATA" I just want excel to search the file given and return a boolean (True/False) as to whether the string exists in the text file. All help is appreciated. Thanks. |
All times are GMT +1. The time now is 08:23 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com