2014-05-11 82 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