CScript or WScript
This is just a quick note on something I had to figure out how to do at work last week: Write a VBScript that always runs in CScript.exe (that is, as a console/shell script, not using the default Windows WScript.exe, which provides GUI handling.
This Usenet thread shows how to determine whether a script is running in CScript or WScript, and even how to force a script to run in one or the other. Unfortunately, it doesn’t run the script with any arguments, and doesn’t seem to work if the script is stored in a directory with any spaces in it. This one does—just use it at the front of your script.
First, this function, which just creates a string with each script argument wrapped in quotation marks:
Private Function argumentList Dim strArgList, strArg strArgList = ""For iX = 0 To WScript.Arguments.Count - 1 strArg = WScript.Arguments(iX) strArgList = strArgList & """" & strArg & """ " NextargumentList = strArgList End Function
Then run the script, with the output of argumentList, in your preferred script host.
strPreferredHost = "CSCRIPT.EXE"'Make sure this is running as a command line script. If UCase(Right(WScript.Fullname, Len(strPreferredHost))) <> UCase(strPreferredHost) Then Set wshShell = WScript.CreateObject("WScript.Shell") wshShell.Run strPreferredHost & " """ & _ WScript.ScriptFullName & """ " & argumentList(), 1, True WScript.Quit End If
Field Reports:
Commenting is closed for this article.

Subscribe to the Daydream Lab [RSS]