Question

I am trying to add the following yes/no box into a batch file but keep failing.

Set objShell = CreateObject("Wscript.Shell")

intMessage = Msgbox("Would you like to go to URL?", _
    vbYesNo, "Click yes to go to URL")

If intMessage = vbYes Then
    objShell.Run("http://www.url.com")
Else
    Wscript.Quit
End If

Can anyone point me in the right direction please?

Was it helpful?

Solution

@echo off

    call :MsgBox "Would you like to go to URL?"  "VBYesNo+VBQuestion" "Click yes to go to URL"
    if errorlevel 7 (
        echo NO - don't go to the url
    ) else if errorlevel 6 (
        echo YES - go to the url
        start "" "http://www.google.com"
    )

    exit /b

:MsgBox prompt type title
    setlocal enableextensions
    set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
    >"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
    set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
    endlocal & exit /b %exitCode%

OTHER TIPS

Although there are several methods to include vbs code into a Batch file, all of them are clumsy, but you may google for they if you want.

May I suggest a different solution?

The inclusion of JScript code into a Batch file is simpler than VBScript, and the translation of a small code segment from VBS to JScript is not problematic. For example:

@if (@CodeSection == @Batch) @then

@echo off
rem Execute the JScript code:
CScript //nologo //E:JScript "%~F0"
goto :EOF

@end

// JScript code start here

objShell = WScript.CreateObject("WScript.Shell")

// http://msdn.microsoft.com/en-us/library/x83z1d9f.aspx
intMessage = objShell.Popup("Would you like to go to URL?",0,"Click yes to go to URL",4) 

if (intMessage == 6) 
    objShell.Run("http://www.url.com")
else
    WScript.Quit()
//endif

I found this code on the Net, perhaps it can help you !

::'.@FILE: .@This: NTFunID031.cmd @Date: 2014-04-17 .@Note: NTMaxToolsPlay ID 031
::'.@SUMMARY: .@Note: cmd+vbs, all in one  .@INFO: .@File: NTFunID031_readme.txt
::'.@PROJECT_NAME: .@Parent: NTMaxTools .@AUTHOR: .@Name: NT 
::'.
::'.
::response= MsgBox("Are you okay ?", vbYesNo+vbQuestion, "Hello ! ;)")
rem assign response to exit code
::wscript.quit(response)
call break '& @echo off& cls& echo.& echo.
call break '& title : -- Real all in one [cmd+vbs], NTMaxToolsPlay, just for the fun ;)
call break '& echo Hi!
call break '& wscript //e:vbs //nologo "%~f0"
call break '& set /a rep=%errorlevel%
call break '& if %rep% equ 6 echo Yesssssssssss, me too :p
call break '& if %rep% equ 7 echo Oh Nonnn :'(
call break '& echo.& pause
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top