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.
VB.NET:
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
Application.Run()
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)
Try
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
Execute_Batch(FILE_NAME)
End If
End If
Catch ex As Exception
strMsg = "Error - OnTimedEvent " & ex.Message
blnStatusGood = False
SendStatusEmail(strMsg)
Exit Sub
End Try
objReader.Dispose()
If FilesFound = True Then
MoveName = MoveDir & "\" & fi.Name
If File.Exists(MoveName) Then
File.Delete(MoveName)
End If
File.Move(FILE_NAME, MoveName)
Debug.Print(vbCrLf & "File Moved")
strMessageBody = strMessageBody & " Batch FIle Sudmitted " & "'" & FILE_NAME & "'" & vbCrLf
End If
Next
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)
aTimer.Stop()
Application.Exit()
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.Start()
batchExecute.WaitForExit(14400000) ' Set for 4 hour max run time
ExitCd = batchExecute.ExitCode
If ExitCd > 0 And Not batchExecute.HasExited Then
blnStatusGood = False
batchExecute.Kill()
Else
blnStatusGood = True
End If
batchExecute.Close()
Exit_Execute_Batch:
Exit Sub
HandleErrors:
strMsg = "Error - Execute_Batch " & Err.Number & ": " & Err.Description
blnStatusGood = False
SendStatusEmail(strMsg)
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 = "Exstream@torchmarkcorp.com"
strSubject = "Exstream Batch Process Run - " & ReportdDate
strEmailRecipients = "WBurke@tmkmail.com"
Try
Dim message As New MailMessage(strFrom, strEmailRecipients)
If blnStatusGood Then
message.Subject = strSubject
Else
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
SendEmailExit:
emailClient = New SmtpClient("mail.torchmarkcorp.com")
emailClient.Send(message)
Catch ex As Exception
Console.WriteLine("Error sending status email: " & ex.Message)
End Try
End Sub
End Module
Last edited: