Command Prompt for Windows




Executing VBScript and JScript

Automation also allows functions and sub routines to be run in another language and the results returned in an environment variable, ready for use in a batch file. The COM object is called "MSScriptControl.ScriptControl". This COM object supports not only VBScript, but also supports JScript as well by default and allows other scripting engines to be installed, such as PerlScript, PythonScript, RubyScript and many others!

The following are examples of calling VBScript from a batch file.

Consider the following simple example :-

COM obj=CreateObject("MSScriptControl.ScriptControl")

COP obj.Language="VBScript"
COM ans=obj.Eval("23 + 45")
ECHO The answer is %ans%

COM obj.Release()

Running the above batch file will display :-

The answer is 68

Firstly, the batch file creates the COM object "MSScriptControl.ScriptControl" and saves the handle to the object in the environment variable obj :-

COM obj=CreateObject("MSScriptControl.ScriptControl")

Next, the object is told what language to use. In this case, it is VBScript :-

COP obj.Language="VBScript"

The .Eval() method is then invoked, passing one parameter, the string "23 + 45". The method then executes the string and returns the answer, which is stored in the environment variable ans :-

COM ans=obj.Eval("23 + 45")

Then the answer is echoed to the screen :-

ECHO The answer is %ans%

and the finally the COM object is released as it is no longer needed :-

COM obj.Release()

The following example demonstrates how to call a VBScript Sub routine from a batch file :-

COM obj=CreateObject("MSScriptControl.ScriptControl")

COP obj.Language="VBScript"

LET crlf=chr(10)+chr(13)

LET s=     "Sub Multiply(a, b)   "+crlf
LET s=s+   "   Dim result        "+crlf
LET s=s+   "   result=a * b      "+crlf
LET s=s+   "   MsgBox result     "+crlf
LET s=s+   "End Sub              "+crlf

COM obj.AddCode(s)
COM obj.Run("Multiply",13.2,2)

COM obj.Release()

Running the above batch file will display the following message :-

Again, the first line creates the COM Object "MSScriptControl.ScriptControl" :-

COM obj=CreateObject("MSScriptControl.ScriptControl")

and the next line sets the COM object property .Language to "VBScript" :-

COP obj.Language="VBScript"

Then the environment variable called crlf is set up to containing the standard Carriage Return and Line Feed character combination used in windows text files :-

LET crlf=chr(10)+chr(13)

Next, the VBSript Sub-routine called Multiply is created and is stored in the environment variable s. Note: each line of text ends with the crlf character combination :-

LET s=     "Sub Multiply(a, b)   "+crlf
LET s=s+   "   Dim result        "+crlf
LET s=s+   "   result=a * b      "+crlf
LET s=s+   "   MsgBox result     "+crlf
LET s=s+   "End Sub              "+crlf

Then the batch file calls the COM object method .AddCode() which has one parameter which is the environment variable s that contains the sub-routine to run :-

COM obj.AddCode(s)

and finally, the COM object method .Run() is called, passing three parameters to the method. The first is the name of the sub-routine to call, then the value for parameter a of the sub-routine and then the value of parameter b for the sub-routine. The method will then run the sub-routine Multiply and display the result in a Message Box and return control back to the batch file :-

COM obj.Run("Multiply",13.2,2)

The last line simply releases the object by calling the COM objects .Release() method :-

COM obj.Release()

It is interesting to note that the above VBScript could also be written as a function and return the result to your batch file a follows :-

COM obj=CreateObject("MSScriptControl.ScriptControl")

COP obj.Language="VBScript"

LET crlf=chr(10)+chr(13)

LET s=     "Function Multiply(a, b)   "+crlf
LET s=s+   "   Multiply=a * b         "+crlf
LET s=s+   "End Function              "+crlf

COM obj.AddCode(s)
COM ans=obj.Run("Multiply",13.2,2)
MSGBOX "Result is" "%ans%"

COM obj.Release()

In this batch file, the .Run() method returns the answer and stores the result in the environment variable ans :-

COM ans=obj.Run("Multiply",13.2,2)

then the batch command MSGBOX is used to display the result :-

MSGBOX "Result is" "%ans%"

Note, functions can also return multi-dimensional arrays, which the WinOne® Command Prompt will store in a multi-dimensional environment variable, as specified.