Solved

search for a string in a text file

Posted on 2006-11-01
7
216 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 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 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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