Search code examples
batch-filecmddos

Show popup window from console


Is possible to show a popup/alert in a Batch file?

I've been reading about vb scripting usage, but it is totally necessary?

http://ss64.com/vb/popup.html or http://ss64.com/vb/msgbox.html


Solution

  • You may use a Batch-JScript hybrid script, that ultimately it is a .BATch file; for example:

    @if (@CodeSection == @Batch) @then
    
    @echo off
    
    CScript //nologo //E:JScript "%~F0" "Hi, there..." 
    goto :EOF
    
    @end
    
    WScript.CreateObject("WScript.Shell").Popup(WScript.Arguments(0));
    

    Copy previous code into a file with .bat extension and execute it. The Popup may also include selection buttons, and the result may be get from the Batch code. For example:

    @if (@CodeSection == @Batch) @then
    
    @echo off
    
    rem Define values for Popup buttons
    set /A YesNoAndCancel=3, QuestionMark=32
    set /A YesButton=6, NoButton=7, TimedOut=-1
    
    rem Call Popup JScript method with a 7 second timeout.
    set /A buttons=YesNoandCancel + QuestionMark
    CScript //nologo //E:JScript "%~F0" "Do you feel alright?" "Answer please:" %buttons% 7
    set btn=%errorlevel%
    if %btn% equ %YesButton% (
       echo Glad to hear you feel alright.
    ) else if %btn% equ %NoButton% (
       echo Hope you're feeling better soon.
    ) else if %btn% equ %TimedOut% (
       echo Is there anybody out there?
    )
    goto :EOF
    
    @end
    
    var arg = WScript.Arguments;
    WScript.Quit(WScript.CreateObject("WScript.Shell").Popup(arg(1),arg(3),arg(0),arg(2)));
    

    For further details about JScript Popup method, see here.