Solved

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

Posted on 2004-03-31
7
354 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now