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

前端 未结 3 1720
故里飘歌
故里飘歌 2020-12-11 06:32

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

相关标签:
3条回答
  • 2020-12-11 07:07

    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
    
    0 讨论(0)
  • 2020-12-11 07:09
    i=(instrrev(ucase(WScript.FullName),"CSCRIPT")<>0)
    

    returns -1 if running cscript, 0 if running wscript

    0 讨论(0)
  • 2020-12-11 07:27

    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.

    0 讨论(0)
提交回复
热议问题