When you are creating scripts to be executed by the Windows Scrip Host, you know there are two applications that could host your script: WScript which is a GUI host or CScript which is a console based host. By default Windows uses WScript, but you can change the default host with a simple command wscript //H:WScript will set WScript to be the default host, and wscript //H:CScript will set CScript to be the host.
But what happens if you don't want to get such an ultimatum for all of your scripts. Wouldn't it be nice if you could make your script choose it's own host? I asked my self that question and found this VBScript code online (I couldn't find the original source):
' Forces this script to be run under the desired scripting host.
' Valid sScriptEng arguments are "wscript" or "cscript".
' If you don't supply a valid name, Force will switch hosts.
Sub Force(sScriptEng)
If Lcase(Right(Wscript.FullName, 12)) = "\wscript.exe" Then
If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then
'Need to switch to CSCRIPT
CreateObject("Wscript.Shell").Run "cscript.exe " & Chr(34) & Wscript.ScriptFullName & Chr(34)
Wscript.Quit
End If
Else
If Instr(1, Wscript.FullName, sScriptEng, 1) = 0 Then
'Need to switch to WSCRIPT
CreateObject("Wscript.Shell").Run "wscript.exe " & Wscript.ScriptFullName
Wscript.Quit
End If
End If
End Sub
All you need to do is call this procedure at the beginning of your script and it will be ready to go. So, for example:
Force "cscript"
will force your script to run on the CScript host. A wrong parameter will just force the script to run on the opposite host.
Set your scripts free!