Vbscript VBX脚本和取消未执行其应执行的操作

Vbscript VBX脚本和取消未执行其应执行的操作,vbscript,Vbscript,我在vbscript中编写了一个代码,它会要求用户输入,然后根据输入运行某些文件,我有else,这样当您键入不属于选项的内容时,它会重新执行if-else序列,但当我尝试按cancel或红色的“X”时,它的行为就好像我输入了无效的内容一样输入并遍历else序列 Dim sInput sInput = InputBox("input") If sInput = "input1" or sInput = "input2" Then set shell=createobject("wscript

我在vbscript中编写了一个代码,它会要求用户输入,然后根据输入运行某些文件,我有else,这样当您键入不属于选项的内容时,它会重新执行if-else序列,但当我尝试按cancel或红色的“X”时,它的行为就好像我输入了无效的内容一样输入并遍历else序列

Dim sInput
sInput = InputBox("input")
If sInput = "input1" or sInput = "input2" Then
   set shell=createobject("wscript.shell")
   shell.run "file.bat"
elseif sInput = "exit" or sInput = "Exit" Then
   WScript.Quit
else
   name=msgbox ("   That is not a valid response",0+16,"ERROR")
   set shell=createobject("wscript.shell")
   shell.run "input.vbs"
end if

不要尝试重新启动脚本

改为使用循环。当用户输入有效选项时结束循环,或者在需要时退出整个程序

Option Explicit

Dim Shell, input, button

Set Shell = CreateObject("WScript.Shell")

Do
  input = InputBox("input")
  If IsEmpty(input) Or LCase(input) = "exit" Then WScript.Quit

  input = LCase(Trim(input))

  If input = "input1" Or input = "input2" Then
    Shell.Run "file.bat"
    Exit Do
  Else
    button = MsgBox("That is not a valid response.", vbExclamation + vbRetryCancel, "ERROR")
    If button = vbCancel Then Exit Do
  End If
Loop
注:

  • Option Explicit
    强制变量声明。始终启用此功能是一个好主意
  • 当用户按下
    输入框中的取消按钮(或Esc键)时,
    IsEmpty()
    为真,但只有在
    LCase
    Trim
    以任何方式操纵响应之前,
    才起作用。支持取消按钮比使用一个特殊的“退出”关键字更直观,所以也许你应该去掉它
  • 可用于
    MsgBox
    的各种常量在中有详细说明。
    您可以使用
    vbDefaultButton1
    vbDefaultButton2
    常量更改Enter和Esc在每个
    MsgBox
    中的操作
  • 无任何条件的
    Do
    循环(
    Do
    /
    在…
    Do
    /
    循环直到…
    )将永远运行-请确保不要忘记使用
    Exit Do
    WScript.Quit()
    。(如果这样做,使用任务管理器终止脚本将使您摆脱困境。)