Solved

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

Posted on 2004-03-31
7
374 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

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