Solved

How can I open a file then search its contents for a string

Posted on 2004-03-31
7
371 Views
Last Modified: 2008-01-16
Please give an example

Must be able to use wildcards

How can I open a file then search its contents for a string.
If the string is found it will immediately stop the search, close the file, then return a True value.
If the string is not found it will close the file then return a False value.
0
Comment
Question by:brianshoenhair
7 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 10727211
Private Sub Command1_Click()
MsgBox FindInFile("mypath & filename", "string to search")
End Sub

Private Function FindInFile(ByVal fnFileName As String, fnSearch As String) As Boolean
Dim myLine As String
FindInFile = False
Open fnFileName For Input As #1
    Do Until EOF(1)
        Line Input #1, myLine
            If InStr(1, myLine, fnSearch) > 0 Then
                FindInFile = True
                Exit Do
            End If
    Loop
Close #1
End Function

S
0
 

Author Comment

by:brianshoenhair
ID: 10727731
Wildcards are a must

Shauli, your example does not work with wildcards
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10727882
Using InStr you don't really need a wildcard, as it looks for any part of a string. However, if your user is using a wildcard, then:

Private Sub Command1_Click()
MsgBox FindInFile("C:\My Documents\products.txt", "sha*")
End Sub

Private Function FindInFile(ByVal fnFileName As String, fnSearch As String) As Boolean
Dim myLine As String
FindInFile = False
Open fnFileName For Input As #1
    Do Until EOF(1)
        Line Input #1, myLine
            If InStr(1, myLine, Replace(fnSearch, "*", "")) > 0 Then
                FindInFile = True
                Exit Do
            End If
    Loop
Close #1
End Function

S
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Shauli
ID: 10727893
Of course:

Private Sub Command1_Click()
MsgBox FindInFile("your path & file", "*your string*")
End Sub

:)

S

0
 
LVL 7

Expert Comment

by:ramesh12
ID: 10727986
C if this is of any help to you

http://www.aivosto.com/regexpr.html
0
 
LVL 4

Accepted Solution

by:
sokolovsky earned 100 total points
ID: 10728159
'Use simple Like with all wildcards like in MS Access
'ex: "test" like "te*t" = True

Option Explicit
Private Sub Form_Load()
    MsgBox FindInFile("C:\boot.ini", "*default*")
End Sub
Private Function FindInFile(ByVal FileName As String, Search As String) As Boolean
Dim strLine As String
FindInFile = False
Open FileName For Input As #1
    Do Until EOF(1)
        Line Input #1, strLine
            If strLine Like Search Then
                FindInFile = True
                Exit Do
            End If
    Loop
Close #1
End Function
0
 

Author Comment

by:brianshoenhair
ID: 10733109
Thank You everyone for your response.

The Like Operator is the best for what I am trying to do.

Points will go to sokolovsky.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question