Solved

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

Posted on 2004-03-31
7
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

615 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