I was doing some code cutting the other day for a customer.
Indirectly.
I had sent a DOS batch file to Customer A, in response for an urgent fix request. The DOS batch file enables Customer A to use a particular web application.
The Help Desk send me an email, "Customer B have this VBscript file which doesn’t work."
Customer A shared the DOS batch file Customer B, who thought they’d have a stab at re-writing it into VBscript.
VBscript file received from Customer B:
Set objWSHShell = WScript.CreateObject("WScript.Shell")
objWSHShell.Run ("dosomething.exe /commandlineparameters", 3, True)
objWSHShell.Run ("dosomething2.exe /commandlineparameters", 3, True)
Now there’s a few problems with this file:
- no checking to see if dosomething.exe exists.
- no checking to see if dosomething.exe & dosomething2.exe return an error.
Here’s my ruggerised, ready for deployment version:
Option Explicit
On Error GoTo 0Dim objWSHShell, objFSO
Dim sDoSomethingParameters, sDoSomethingFilePathName, sDoSomethingCommandString
Dim boolFileExists, boolResult
Dim intDoSomethingResult
Const intWindowStyleHideAndActivate = 0
sDoSomethingFilePathName = "C:\doer\DoSomething.exe"
boolResult = False
intDoSomethingResult = 555
Set objWSHShell = WScript.CreateObject("WScript.Shell")
Call fn_CheckIfFileExists(sDoSomethingFilePathName, boolFileExists)
If Not boolFileExists Then
' exit with File Does Not Exist
WScript.Quit (2318)
End If
sDoSomethingParameters = " -q -a qwerty"
sDoSomethingCommandString = sDoSomethingFilePathName + sDoSomethingParameters
intDoSomethingResult = objWSHShell.Run (sDoSomethingCommandString, intWindowStyleHideAndActivate, True)
If intDoSomethingResult <> 0 Then
' execution of DoSomething and parameters failed.
WScript.Quit (1708)
End If
' if we're gotten here, it's worked, and we can exit the script!
Wscript.quit (0)
~~~~~~~
Function fn_CheckIfFileExists(fnFilePathName, boolResult)
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(fnFilePathName)
On Error GoTo 0
If objFSO.FileExists(fnFilePathName) Then
boolResult = True
Else
boolResult = False
End If
Set objFSO = Nothing
Set objFile = Nothing
End Function
I’m reminded of the maxim:
Customers like to (re)use the methods of the support team, but they don’t really understand them.
If you click on the picture, you’ll get to view the powerpoint file, where I saw the original maxim.