我正在尝试在记事本中创建一个非常简单的.vbs,它将执行以下操作,但我遇到了一些麻烦,因为我对脚本有点新:
如果选择“是”,则执行.bat,然后关闭窗口,如果选择“否”则不执行任何操作。
显示一条消息,以便您知道为什么要点击“是”或“否”。
显示窗口标题。
这是我到目前为止试图让自己做的事情:
x=msgbox("MESSAGE HERE",4,"WINDOW TITLE HERE")
const Hidden = 0
const WaitOnReturn = true
set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%HOMEPATH%\Documents\FOLDER\FOLDER\EXAMPLE.BAT", Hidden, WaitOnReturn
WScript.Echo "Done"
它工作得很好,但是,即使我选择“否”,它仍然会执行.bat,这是我不想要的。
答案 0 :(得分:0)
试试这段代码:
Option Explicit
Const Hidden = 0
Const WaitOnReturn = True
Dim Question,BatchFilePath,Message,Title,Result
Title = "Running a .bat through a .vbs"
Message = "Did you want to continue executing this script"
BatchFilePath = "%ProgramFiles%\FolderTest\Folder Name with spaces\EXAMPLE.BAT"
'We add the double quotes in this variable to bypass spaces issues in the path
BatchFilePath = DblQuote(BatchFilePath)
Question = Msgbox(Message,VbYesNo + VbQuestion,Title)
If Question = VbNo Then
MsgBox "You have chosen to quit this script !",vbExclamation,Title
WScript.Quit() ' We quit the script
Else
Result = Run(BatchFilePath,Hidden,WaitOnReturn)
End If
'*********************************************************************
Function Run(StrCmd,Console,bWaitOnReturn)
Dim ws,MyCmd,Result
Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the MS-DOS console
If Console = 0 Then
MyCmd = "CMD /C " & StrCmd & ""
Result = ws.run(MyCmd,Console,bWaitOnReturn)
If Result = 0 Then
MsgBox "Success",VbInformation,Title
Else
MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
End If
End If
'A value of 1 to show the MS-DOS console
If Console = 1 Then
MyCmd = "CMD /K " & StrCmd & ""
Result = ws.run(MyCmd,Console,bWaitOnReturn)
If Result = 0 Then
MsgBox "Success",VbInformation,Title
Else
MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
End If
End If
Run = Result
End Function
'*********************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************
答案 1 :(得分:0)
试试这个:
Set obj = CreateObject("WScript.shell")
Answer = MsgBox("Content Here",vbYesNo,"Title Here")
If Answer = vbYes Then
obj.Run "PATH TO BATCH FILE"
Else
WScript.Quit 0
End If