Solved

search for a string in a text file

Posted on 2006-11-01
7
209 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 46
Vb. Net application freezes 9 46
VB.NET 1 22
get row value in vb.net 4 14
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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