RunTraceRT

and determine running time

This script runs trace route as an external command. It then parses the command line output by reading stdout which is redirection from the cmd prompt. Now we simply echo it out by using wscript.echo. The key to this program is using wscript.shell, and the exec method.

The cool thing is using a function to tell you how long the program has been running. Now VBScript has a timer function built in, but I did this just to play with datepart and to work with passing multiple variables into a function. Note that when you do this you simply call the name of the function and in () you supply the variables seperated by a comma. On the function line, you then use the function command followed by the name of your function and the two variables it is expecting.

' NAME: <RunTraceRTdetermineRunTime.vbs>
'
' COMMENT: Key concepts are listed below:
'1. using the Wscript.shell exec method to run programs
'2. using the StdOut method to capture output
'3. using wscript.echo to return the contents of StdOut
'==========================================================================

Option Explicit ' is used to force the scripter to declare variables
On Error Resume Next ' is used to tell vbscript to go to the next line if it encounters an Error
Dim objShell
Dim objExecObject
Dim strText
Dim startTime, EndTime

startTime = Now
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c Tracert 127.0.0.1")

Do until objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
Wscript.Echo strText
Loop
endTime = Now

WScript.echo "Script Complete. It took " & DetermineRunTime (startTime, EndTime) & " to run"

Function DetermineRunTime (startTime, EndTime)
Dim Smins, Ssecs ' used for start time min. and sec
Dim Emins, Esecs ' used for end time min. and sec
Dim totalMins ' used to determine total minutes
Dim totalSecs ' convert total minutes and add seconds
Dim minsecs ' holds conversion from minutes to seconds

Smins = DatePart ("n", startTime) ' n is used by datepart for minutes
Ssecs = DatePart ("s", startTime) ' s is used by datepart for seconds
Emins = DatePart ("n", endTime)
Esecs = DatePart ("s", endTime)
totalMins = Emins - Smins
minSecs = TotalMins * 60
totalSecs = (minSecs + Esecs) - Ssecs

DetermineRunTime = totalSecs & " secs"
End Function