Solved

search for a string in a text file

Posted on 2006-11-01
7
223 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
[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 63

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 86

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 86

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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