3

How do I force the vbs script to run in the cscript host as opposed to the WScript host?

How do I go about reliably determining if vbs is running from the Command Prompt in XP/Vista/7?

Also, if its not running from the command prompt, how would I get the script to launch itself into command prompt?

I'm looking for a short snippet.

Eugene
  • 9,015
  • 18
  • 58
  • 86
  • Voting to close this as this sounds more like a question for scripting... – t0mm13b Jan 22 '11 at 01:12
  • programming, scripting, same difference. VBScript is used in ASP all the time although it doesn't use the command line in ASP. – HK1 Jan 22 '11 at 01:19
  • Running from the Command Prompt as opposed to what? – anon Jan 22 '11 at 03:26
  • As opposed to the VBS host WScript, I'd like to force the script to run in CScript – Eugene Jan 22 '11 at 15:50
  • 1
    possible duplicate of [Force a VBS to run using cscript instead of wscript](http://stackoverflow.com/questions/4692542/force-a-vbs-to-run-using-cscript-instead-of-wscript) – Cody Gray Jan 23 '11 at 04:13

1 Answers1

3

There is no property or anything like that you can set, so you are left with ugly hacks like this:

Function ForceCScript()
On Error Resume Next
WScript.StdErr.Write(Chr(7))
If Err.Number <> 0 Then
    Err.Clear
    On Error GoTo 0
    set WshSh=WScript.CreateObject("WScript.Shell")
    sh=WshSh.ExpandEnvironmentStrings("%COMSPEC%")
    If InStr(sh,"%") = 1 Then sh="cmd.exe"
    WshSh.Run(sh&" /K cscript /nologo """&WScript.ScriptFullName&"""")
    WScript.Quit()
End If
End Function

call ForceCScript()
Anders
  • 83,372
  • 11
  • 96
  • 148
  • No environment variable...I was afraid of that =(. Thanks for your response. I've also found a similar question at http://stackoverflow.com/questions/4692542/force-a-vbs-to-run-using-cscript-instead-of-wscript . Also it should be //nologo per http://technet.microsoft.com/en-us/library/ee156587.aspx .Thanks! – Eugene Jan 23 '11 at 01:18