This section provides a tutorial example on how to run a VBScript file with or without 'cscript/wscript' command.
Now let's write and run our first VBScript with WSH.
Create a VBScript example file, Hello_Echo.vbs, with the following statements:
' Hello_Echo.vbs
'- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
Set oArgs = WScript.Arguments
If oArgs.Length = 0 Then WScript.Echo "Hello wolrd!"
For Each sArg In oArgs
WScript.Echo "Hello " & sArg
Next
Run Hello_Echo.vbs in many ways in a command window:
C:\herong>cscript Hello_Echo.vbs Herong Tom
Hello Herong
Hello Tom
C:\herong>wscript Hello_Echo.vbs Herong Tom
(Dialog box showing "Hello Herong")
(Dialog box showing "Hello Tom")
C:\herong>cscript //h:cscript
The default script host is now set to "cscript.exe".
C:\herong>Hello_Echo.vbs Herong Tom
Hello Herong
Hello Tom
C:\herong>cscript //h:wscript
The default script host is now set to "wscript.exe".
C:\herong>Hello_Echo.vbs Herong Tom
(Dialog box showing "Hello Herong")
(Dialog box showing "Hello Tom")
Noticed that Hello_Echo.vbs can be executed in 3 ways:
"cscript Hello_Echo.vbs ..." - Using host cscript.exe to execute.
"wscript Hello_Echo.vbs ..." - Using host wscript.exe to execute.
"Hello_Echo.vbs ..." - Using the default host, cscript.exe or wscript.exe.
You can change the default host using the "cschipt //h:..." command.