Question Error checking shell comands in console applacations


Active member
Feb 3, 2009
Programming Experience
I have a console application that watch’s a directory for a file. When it see a file it opens the file reads each line and submits them to DOS. The problem I am having is that when I get a error the application returns and does not know or report the error. In this case I and getting a file not found error but do not get a error when it returns. Thank you in advance for your help.
Imports System
Imports System.IO
Imports System.Timers
Imports System.Diagnostics
Imports System.Net.Mail

Module Module1

    ' Set the watch Directory here
    Dim DIR_NAME As String = "\\server\122nd_PA_PGM\Exstream\AutoBatchRun"
    Dim di As New IO.DirectoryInfo(DIR_NAME)
    Dim FILE_NAME As String

    ' Set the Archive Directory here 
    Dim MoveDir As String = "\\server\122nd_PA_PGM\Exstream\AutoBatchArchive"
    Dim MoveName As String

    ' Set the Daily end time here
    Dim EndTime As DateTime = #1:00:00 PM#

    Dim aTimer As System.Timers.Timer
    Dim StopSwitch As Boolean = False

    Dim strMsg As String
    Dim blnStatusGood As Boolean = True
    Dim strMessageBody As String

    Sub Main()

        Debug.Print("Application Begin time : " & Now)

        ' Create a timer with a 2 second interval.
        aTimer = New System.Timers.Timer(2000)

        ' Hook up the Elapsed event for the timer.
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

        ' Set the Interval to 3 min for testing (60000 milliseconds = 1 min).
        aTimer.Interval = 180000
        'aTimer.Interval = 2000
        aTimer.Enabled = True


    End Sub

    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.bat")
        Dim fi As IO.FileInfo
        Dim TextLine As String
        Dim FilesFound As Boolean = False

        Debug.Print("The Elapsed event was raised at " & e.SignalTime)

        For Each fi In aryFi

            Debug.Print("File Name: " & fi.Name)
            FILE_NAME = " "
            FILE_NAME = DIR_NAME & "\" & fi.Name

            '** Open/Read the Batch file
            Dim objReader As New System.IO.StreamReader(FILE_NAME)

                If System.IO.File.Exists(FILE_NAME) = True Then
                    FilesFound = True
                    TextLine = objReader.ReadLine()
                    If Mid(TextLine, 1, 11) = "END-PROGRAM" Then
                        StopSwitch = True
                    ElseIf (Mid(TextLine, 1, 42) = "\\o2Exstream\Exstream_Engine_V8\ProdEngine") _
                        Or (Mid(TextLine, 1, 5) = "Copy") Then
                    End If
                End If

            Catch ex As Exception
                strMsg = "Error - OnTimedEvent " & ex.Message
                blnStatusGood = False
                Exit Sub

            End Try


            If FilesFound = True Then
                MoveName = MoveDir & "\" & fi.Name
                If File.Exists(MoveName) Then
                End If
                File.Move(FILE_NAME, MoveName)
                Debug.Print(vbCrLf & "File Moved")
                strMessageBody = strMessageBody & "  Batch FIle Sudmitted " & "'" & FILE_NAME & "'" & vbCrLf
            End If

        If blnStatusGood = True And FilesFound = True _
        Or StopSwitch = True Then
            SendStatusEmail(" ")
        End If

        Dim Runtime As DateTime
        Runtime = TimeOfDay
        If TimeOfDay > EndTime _
        Or StopSwitch = True Then
            Debug.Print("Application Stoped" & Now)
            Exit Sub
        End If

    End Sub

    Sub Execute_Batch(ByVal Batch_file As String)
        On Error GoTo HandleErrors

        'Dim sYourCommand As String
        'ChDir(Dir1) 'since Dir1 is the current Directory
        'sYourCommand = "dir " & Dir1 & "> " & Dir1 & "\index.txt"
        'Shell("cmd /c " & sYourCommand, vbHide)

        Dim batchSuccessful As Integer
        Dim ExitCd As Integer

        Dim batchExecute As New Process()
        Dim batchExecuteInfo As New ProcessStartInfo()

        batchExecuteInfo.UseShellExecute = False

        ' Ued to hide the dos window "True = No Window"
        batchExecuteInfo.CreateNoWindow = True

        batchExecute.StartInfo = batchExecuteInfo
        batchExecute.StartInfo.FileName = Batch_file

        batchExecute.WaitForExit(14400000) ' Set for 4 hour max run time

        ExitCd = batchExecute.ExitCode
        If ExitCd > 0 And Not batchExecute.HasExited Then
            blnStatusGood = False
            blnStatusGood = True
        End If


        Exit Sub


        strMsg = "Error - Execute_Batch " & Err.Number & ": " & Err.Description
        blnStatusGood = False
        GoTo Exit_Execute_Batch

    End Sub

    Private Sub SendStatusEmail(ByVal strMsg As String)
        Dim emailClient As SmtpClient
        Dim ReportdDate As Date
        Dim strFrom As String
        Dim strSubject As String
        Dim strEmailRecipients As String

        ReportdDate = Now
        strFrom = ""
        strSubject = "Exstream Batch Process Run - " & ReportdDate
        strEmailRecipients = ""

            Dim message As New MailMessage(strFrom, strEmailRecipients)

            If blnStatusGood Then
                message.Subject = strSubject
                message.Subject = strSubject & " - ** ERROR ** "
                strMessageBody = "Extream Batch Run Completed - " & Now() & vbCrLf & vbCrLf
                strMessageBody = strMessageBody & "Extream Batch Run - ** ERROR ** - See Details Below." & vbCrLf & vbCrLf
                strMessageBody = strMessageBody & "----- Status Log Details -------" & vbCrLf
                message.Body = strMessageBody & strMsg
                GoTo sendEmailExit
            End If

            If StopSwitch = True Then
                strMessageBody = strMessageBody & vbCrLf & "***** Extream Batch Process Stoped *****"
            End If

            strMessageBody = strMessageBody
            message.Body = "Extream Batch file Run Completed - " & Now() & vbCrLf & vbCrLf & strMessageBody


            emailClient = New SmtpClient("")

        Catch ex As Exception
            Console.WriteLine("Error sending status email: " & ex.Message)

        End Try

    End Sub

End Module
Last edited:


Well-known member
Oct 18, 2011
Montreal, QC, CA
Programming Experience
First off, your whole "read a file line by line and submit each line to DOS" makes no sense. If it's a batch file, give it a .BAT or .CMD extension and just run it. Batch files also have provisions to check application exit codes and make conditional decisions.

Second, if you have a batch file, and you want to run it on a schedule, you use the Windows Task Scheduler, which is located in your control panel. If you need to add a scheduler job from a VB.Net application, that is possible too, there are even wrappers out there that abstract most of the heavy work in a nice .NET library.


Active member
Feb 3, 2009
Programming Experience
The error message was
W:\vbprojects>D:\Exstream_Engine\ProdEngine -controlfile=D:\Exstream\control.opt
The error message file - D:\Exstream_Engine\MsgResource_en-us.dat - could not be found.
Looking at the error message I see that I was mistaken on my first assessment the program returned the error message. I wonder if I could pipe this into a txt file then read it and determine if there was an error or at least put it in the email. Unless someone else can steer me to a way to capture DOS error messages.
Top Bottom