How to tell the difference between a VBscript is run from command line or by clicking it in a window?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 08:46:49

问题


All I want to do is differentiate between the program being run by the command line or by clicking the test.vbs file in a window.

If you run the script by typing C:\testFolder\test.vbs in a command prompt, then I want the program to run differently than if you double clicked test.vbs in the testFolder.

Is there some system variable that I can use to differentiate between the two scenarios? I first attempted to use WScript.Fullname to determine if the pathname ended in cscript or wscript. But that didn't work so well.

Any ideas are greatly appreciated.


回答1:


You could try something like this:

Set WshShell = CreateObject("WScript.Shell")
Set objEnv = WshShell.Environment("Process")

msgbox objenv("PROMPT")

In general PROMPT will be set to something like $P$G when run from a command prompt, but left blank when you run the .VBS file directly.




回答2:


If you want to test against WScript.FullName, you can use InStr with vbTextCompare so that the match is case-insensitive.

If InStr(1, WScript.FullName, "cscript", vbTextCompare) Then
    WScript.Echo "Console"
ElseIf InStr(1, WScript.FullName, "wscript", vbTextCompare) Then
    WScript.Echo "Windows"
Else
    WScript.Echo "???"
End If



回答3:


i=(instrrev(ucase(WScript.FullName),"CSCRIPT")<>0)

returns -1 if running cscript, 0 if running wscript



来源:https://stackoverflow.com/questions/4571816/how-to-tell-the-difference-between-a-vbscript-is-run-from-command-line-or-by-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!