Solved

search for a string in a text file

Posted on 2006-11-01
7
204 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
ID: 17855103
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
ID: 17855256
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
ID: 17858223
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

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

Expert Comment

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

Expert Comment

by:Mike Tomlinson
ID: 17860452
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
ID: 17863403
Thanks, works great. Chad
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

20 Experts available now in Live!

Get 1:1 Help Now