N2

Meh

How to execute vb scripts using cscript instead of wscript

with one comment

Ever double clicked a vbscript file only to realize later that the script is now executing under wscript instead of cscript as it was intended to. The difference for me is that wscript is more annoying as all the output or status messages from the script now show up as modal message box and the script waits untill you press ok to proceed forward. However there are a couple of ways to avoid this.

The easiest way is to set cscript as you default script engine. At the command prompt type in

cscript /h:cscript
or
wscript /h:cscript

cscript /h:cscript

This sets the default engine to csript. Now if you double click a file it will execute under cscript engine and all the status messages show up in a command window instead of those annoying pop up message boxes.

The other way is to build the functionality within your script to let it choose the host scripting engine. To do this add the following sub to your script and call this sub before executing anything else. It will check the host and if it is cscript proceed as normal, if not, it will re launch itself in a new command window using cscript. I find it handy when I distribute scripts to my client where I dont have control over the default script engine setting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sub ForceCSCRIPT()
	Dim oShell, strCmd,arg
	Set oShell = CreateObject("Wscript.Shell")
 
	If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
	  oShell.Popup "Launched using wscript. Re-launching using cscript ...",3,"WSCRIPT"
	  strCmd="cmd.exe /k " &_
              WScript.Path & "\cscript.exe //NOLOGO " &_ 
             Chr(34) & WScript.scriptFullName & Chr(34)
	  For Each arg In WScript.arguments
		strCmd = strCmd & " " & arg
	  Next
	  oShell.Run strCmd,1,False
	  WScript.Quit 0
	End If
 
End Sub

Written by Singh

August 17th, 2010 at 9:33 am