Friday, September 12, 2008

Back Run Console Program which deal with parsing date parameters..

Sometimes we wrote console programs which taking in a date as a parameter to query or do some actions for a particular date. But what if you wanted to back run for a range of date and don't want to change the design of the original console program or you don't have access to the code of the console program? Write a wrapper program!! =) Of course your original console program must be able to take in the date as a parameter.

How to use?

Backrun.exe 01/09/2008 31/09/2008

This will run the console program you specify inside the code from 1st to 31st of September 2008.

Module BackRun

Sub Main(ByVal args As String())
Dim StartDate As Date
Dim EndDate As Date
Dim NumberOfDays As Integer
Dim counter As Integer
Dim i As Integer
Dim run As String = "D:\YourConsoleProgram.exe"
i = 0
If args.Length <> 2 Then
Console.WriteLine("Please enter from date and to date to define back run period")
Else
For Each backDate As String In args
If i = 0 Then
StartDate = DateAndTime.DateValue(backDate)
End If
EndDate = DateAndTime.DateValue(backDate)
i += 1
Next
If DateTime.Compare(StartDate, EndDate) > 0 Then
Console.WriteLine("Start Date should be earlier than End Date")
Else
NumberOfDays = EndDate.Subtract(StartDate).Days
Console.WriteLine("Number of Days " & NumberOfDays)
For counter = 0 To NumberOfDays
Try
Shell(run & " " & StartDate.AddDays(counter), AppWinStyle.MaximizedFocus, True)
Catch ex As Exception
Console.Write(ex.Message)
End Try
Next counter
End If

End If
End Sub

End Module

No comments: