Solved

search for a string in a text file

Posted on 2006-11-01
7
192 Views
Last Modified: 2010-04-23
I would like to search for "Operation : Repair" in a text file.  If I find this string in the text file I would like to set a flag on a variable (true/false)?  How do I search for a string like this in a text file?  Thanks, Chad
0
Comment
Question by:chadmanvb
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi chadmanvb;

This should do it.

Imports System.Text.RegularExpressions

        ' Test input string
        Dim input As String = "text file I would like to set a flag on a variable (true/false)?  How do I search for a" & vbCrLf & "I would like to search for ""Operation : Repair"" in a text file.  If I find this string in the" & "string like this in a text file?  Thanks, Chad"

        Dim pattern As String = "Operation : Repair"
        Dim flag As Boolean = False

        ' Flag will be true if "Operation : Repair" is in the string otherwise false
        flag = Regex.IsMatch(input, pattern)


Fernando
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
Comment Utility
Something like...

        Dim sr As New System.IO.StreamReader("c:\someFile.txt")
        Dim contents As String = sr.ReadToEnd
        sr.Close()
        Dim valuePresent As Boolean = (contents.IndexOf("Operation : Repair") <> -1)
0
 
LVL 3

Expert Comment

by:vsvb
Comment Utility
esay way is this

  Dim objFSO As Object
        Dim objfile As Object
        Dim str As String
        objFSO = CreateObject("Scripting.FileSystemObject")
        objfile = objFSO.OpenTextFile("C:\Test.txt")
        str = objfile.ReadAll
        objFile.Close()
        If InStr(str, "Operation : Repair", CompareMethod.Text) Then
            MsgBox("hi got it")
        Else
            MsgBox("sorry")
        End If
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
vsvb...why are you suggesting FSO when this is can be done natively in VB.Net?...
0
 
LVL 3

Expert Comment

by:vsvb
Comment Utility
you are right ... some time i need to get rid of vb6 way  
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Don't get me wrong...there is nothing technically wrong with it.  =)

I still use some of the old VB6 ways.  For instance, the VB6 Split() method does some things that the String.Split() method can't, and sometimes I like to use the old CallByName() vs. Reflection, etc...

By the way, VB.Net 2005 can do it with a one-liner:
   
    Dim valuePresent As Boolean = My.Computer.FileSystem.ReadAllText("c:\someFile.txt").Contains("Operation : Repair")
0
 

Author Comment

by:chadmanvb
Comment Utility
Thanks, works great. Chad
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

9 Experts available now in Live!

Get 1:1 Help Now