Msgbox in Batch Files

The following VBScript file will display a msgbox with properties based on the arguments passed to this file, then return an error code based on which button the user pressed.
msg.vbs

set args=WScript.Arguments
Dim text,style,title,return
text=args.item(0)
style=args.item(1)
title=args.item(2)
x=msgbox(text,style,title)
if x=vbOK then
return=1
elseif x=vbCancel then
return=2
elseif x=vbAbort then
return=3
elseif x=vbRetry then
return=4
elseif x=vbIgnore then
return=5
elseif x=vbYes then
return=6
elseif x=vbNo then
return=7
end if
wscript.quit return

Cmd command:

cscript /nologo msg.vbs "hello world!" 65 "Example"

As shown above, you will have to run msg.vbs with cscript.exe in order to get the proper errorlevel value relative to the button the user pressed on the msgbox.
The %errorlevel% value will be set depending on what button the user presses on the msgbox:

OK = 1
Cancel = 2
Abort = 3
Retry = 4
Ignore = 5
Yes = 6
No = 7

The following batch file will tell you which button you pressed on a msgbox that contains the buttons and icons you specify.
Test.bat:

@echo off
:main
cls
echo.
echo Select msgbox buttons:
echo.
echo 0) OK Only
echo 1) OK and Cancel
echo 2) Abort, Retry, Ignore
echo 3) Yes, No, Cancel
echo 4) Yes and No
echo 5) Retry and Cancel
echo.
set /p b=
cls
echo.
echo Select msgbox icons:
echo.
echo 16) Critical
echo 32) Question
echo 48) Exclamation
echo 64) Information
echo.
set /p i=
set /a style=%b%+%i%
cls
cscript /nologo msg.vbs "Press any button." %style% "msgbox"
set button=%errorlevel%
call :getval
setlocal enabledelayedexpansion
echo You pressed the !%button%! button.
endlocal
pause
goto main
:getval
set 1=OK
set 2=Cancel
set 3=Abort
set 4=Retry
set 5=Ignore
set 6=Yes
set 7=No
goto :eof

2 thoughts on “Msgbox in Batch Files

    • A newbie needs a little help jakash3. Please. I’m creating a batch that will create a msgbox with multiple lines of texts and title. How would I go about doing that?

      This is one example:
      echo x=msgbox(“text” ,64,”title”)>msg.vb
      cscript msg.vb

      So, how would I add more lines of text and keep batch file compatibility?

      Thx in advance. jairo1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: