2014-05-11 47 views
0

我试图在记事本中一个非常简单的.vbs,将做到以下几点,但我有一点麻烦,因为我是一个小新的脚本:通过一个.vbs运行.BAT

  • 如果选择“是”,则执行.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,我不要。

+1

所以加一个'if'测试用户输入的内容。 –

+0

非常感谢您的回复,Marc! 您是否介意我将如何将其集成到我当前的脚本中?或者如果我能做得更好/更容易? Regards, Patrick – ajdbnabad13

+0

http://msdn.microsoft.com/en-us/library/5h27x7e9%28v=vs.84%29.aspx –

回答

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 
'********************************************************************* 
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