将vbs是/否框代码添加到批处理文件中

时间:2014-05-07 23:39:32

标签: batch-file vbscript

我正在尝试将以下是/否框添加到批处理文件中但仍然失败。

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

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:2)

@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%

答案 1 :(得分:1)

虽然有几种方法可以将vbs代码包含在批处理文件中,但所有这些方法都很笨拙,但如果需要,可以google for they

我可以建议一个不同的解决方案吗?

将JScript代码包含在批处理文件中比VBScript简单,并且从VBS到JScript的小代码段的转换不成问题。例如:

@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

答案 2 :(得分:1)

我在网上找到了这个代码,也许它可以帮助你!

::'.@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