none
Need help with VB on how to search through text files contents for astrisks and then send email notification. RRS feed

  • Question

  • How can I search throught every txt file in the c:\temp folder for "**", if two astrisks are found it will send an email?

    Imports System.IO
    
    Module Module1
    
        Sub Main()
    
            Dim srchLine As String
            Dim astriskLoc As String = "c:\temp\"
    
    
    
            'Read from the directory content
            Dim finfo As New DirectoryInfo(astriskLoc)
    
            For Each fi In finfo.GetFiles("*.txt")
    
    
                srchLine = File.ReadAllText(fi.FullName)
    
                'Continue to read until you reach the end of the file.
                Do While srchLine <> "**"
    
                Loop
    
                Console.ReadLine()
            Next
    
    
        End Sub
    
    End Module

    I appreciate any help, thanks in advance.


    Wednesday, March 14, 2018 5:44 PM

Answers

  • Imports System.IO Module Module1 Sub Main() Dim srchLine As String Dim astriskLoc As String = "c:\temp\" 'Read from the directory content' Dim finfo As New DirectoryInfo(astriskLoc) For Each fi In finfo.GetFiles("*.txt") srchLine = File.ReadAllText(fi.FullName) If srchLine.IndexOf("**") <> -1 Then 'Send mail' End If Next

    End Sub End Module

    I forgot to tell, don't use the word script, it took probably so long before you got a reply because everybody thought you were using VBS for which is another forum. Somehow it seems there was no moderator available to handle this.

    Success
    Cor




    Wednesday, March 14, 2018 6:51 PM
  • Hello,

    Strictly addressing SMTP email. The link below has simple to advance methods to send email messages.

    http://www.systemnetmail.com/default.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Matt_90 Thursday, March 15, 2018 5:28 AM
    Wednesday, March 14, 2018 8:18 PM
    Moderator
  • Also, instead of reading the entire file you could just read character-by-character looking for the asterisks.  This provides the ability to quit reading the file as soon as two asterisks are found.  It also avoids allocating potentially large strings to read the entire file.  This could provide a significant performance increase if any of the files are large or if you expect more files will have asterisks than won't.

    There's no reason you can't use the ReadAllText method (assuming none of the files are really huge - e.g. gigabytes in size).  This is just an alternative in case you need the potential performance increase.

    Dim astriskLoc As String = "c:\temp\"
    
    'Read from the directory content
    Dim finfo As New DirectoryInfo(astriskLoc)
    
    'iterate over each file
    For Each fi In finfo.GetFiles("*.txt")
        'open the file for reading
        Using reader = New IO.StreamReader(IO.File.OpenRead(fi.FullName))
            'declare temp variables for analysis
            Dim c(0) As Char
            Dim count As Integer = 0
            'iterate over charcters in the stream
            Do While Not reader.EndOfStream
                'read a character
                reader.Read(c, 0, 1)
                If c(0) = "*"c Then
                    'if it is *, add 1 to count
                    count += 1
                Else
                    'if not, reset count to 0
                    count = 0
                End If
                'if count = 2 then two * were found in a row
                If count = 2 Then
                    'TODO: send email
                    'exit this file and start the next
                    Exit Do
                End If
            Loop
        End Using
    
        Console.ReadLine()
    Next
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Matt_90 Thursday, March 15, 2018 4:52 AM
    Wednesday, March 14, 2018 7:40 PM
    Moderator

All replies

  • Imports System.IO Module Module1 Sub Main() Dim srchLine As String Dim astriskLoc As String = "c:\temp\" 'Read from the directory content' Dim finfo As New DirectoryInfo(astriskLoc) For Each fi In finfo.GetFiles("*.txt") srchLine = File.ReadAllText(fi.FullName) If srchLine.IndexOf("**") <> -1 Then 'Send mail' End If Next

    End Sub End Module

    I forgot to tell, don't use the word script, it took probably so long before you got a reply because everybody thought you were using VBS for which is another forum. Somehow it seems there was no moderator available to handle this.

    Success
    Cor




    Wednesday, March 14, 2018 6:51 PM
  • Also, instead of reading the entire file you could just read character-by-character looking for the asterisks.  This provides the ability to quit reading the file as soon as two asterisks are found.  It also avoids allocating potentially large strings to read the entire file.  This could provide a significant performance increase if any of the files are large or if you expect more files will have asterisks than won't.

    There's no reason you can't use the ReadAllText method (assuming none of the files are really huge - e.g. gigabytes in size).  This is just an alternative in case you need the potential performance increase.

    Dim astriskLoc As String = "c:\temp\"
    
    'Read from the directory content
    Dim finfo As New DirectoryInfo(astriskLoc)
    
    'iterate over each file
    For Each fi In finfo.GetFiles("*.txt")
        'open the file for reading
        Using reader = New IO.StreamReader(IO.File.OpenRead(fi.FullName))
            'declare temp variables for analysis
            Dim c(0) As Char
            Dim count As Integer = 0
            'iterate over charcters in the stream
            Do While Not reader.EndOfStream
                'read a character
                reader.Read(c, 0, 1)
                If c(0) = "*"c Then
                    'if it is *, add 1 to count
                    count += 1
                Else
                    'if not, reset count to 0
                    count = 0
                End If
                'if count = 2 then two * were found in a row
                If count = 2 Then
                    'TODO: send email
                    'exit this file and start the next
                    Exit Do
                End If
            Loop
        End Using
    
        Console.ReadLine()
    Next
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Matt_90 Thursday, March 15, 2018 4:52 AM
    Wednesday, March 14, 2018 7:40 PM
    Moderator
  • Hello,

    Strictly addressing SMTP email. The link below has simple to advance methods to send email messages.

    http://www.systemnetmail.com/default.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Matt_90 Thursday, March 15, 2018 5:28 AM
    Wednesday, March 14, 2018 8:18 PM
    Moderator
  • Thank you all and I appreciate your help.

    Thursday, March 15, 2018 5:29 AM