Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default 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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default 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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default 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.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search If on text string bob Excel Worksheet Functions 3 February 15th 10 06:49 PM
search text string for number Sooz Excel Discussion (Misc queries) 10 August 24th 09 04:20 PM
Nested formula to search a text string and return specific text Barbie Excel Worksheet Functions 10 February 21st 09 07:40 AM
Search for a text string Dan Excel Discussion (Misc queries) 1 November 10th 08 09:09 PM
Search, find or lookup defined text in text string zzxxcc Excel Worksheet Functions 9 September 6th 07 09:37 PM


All times are GMT +1. The time now is 03:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"