【问题标题】:How can you create pop up messages in a batch script?如何在批处理脚本中创建弹出消息?
【发布时间】:2012-09-12 22:17:10
【问题描述】:

我需要知道如何不使用使用 VBScript 或 KiXtart 或任何其他外部脚本/编程语言在批处理脚本中制作弹出消息。 我对此一无所知……甚至没有起点。 我知道NET SEND,但在我当前的环境中禁用了 Messenger 服务。

【问题讨论】:

标签: popup batch-file


【解决方案1】:
msg * "Enter Your Message"

这有帮助吗?

【讨论】:

  • 适用于 Windows 7 但不适用于 10
  • @M. Mimpen 这里是我的 virtualbox 安装截图:i.imgur.com/blWLxdH.png
  • @GM-Script-Writer-62850 显然,它只安装在商业版和更高版本上。来源:superuser.com/questions/269880/how-can-i-install-msg-exe
  • msg.exe 不附带 Windows 10 家庭版,但是您可以将 exe 和 exe.mui 文件从专业版复制到家庭版。
  • 如何暂停脚本直到消息框被关闭?
【解决方案2】:

关于 LittleBobbyTable 的回答 - NET SEND 在 Vista 或 Windows 7 上不起作用。它已被 MSG.EXE 取代

有一个粗略的解决方案适用于所有版本的 Windows - 可以通过启动一个新的 cmd.exe 窗口来发送粗略的弹出消息,该窗口在按下一个键后会关闭。

start "" cmd /c "echo Hello world!&echo(&pause"

如果您希望脚本在消息框消失之前暂停,那么您可以添加 /WAIT 选项。

start "" /wait cmd /c "echo Hello world!&echo(&pause"

【讨论】:

  • 嗯,我什至没有想到这一点。如此原始而有效。谢谢!
  • 为什么是echo(&echo.& 不也能正常工作吗?并且没有不平衡的括号。
  • 事实证明ECHO. 在某些情况下会失败,而ECHO( 永远不会失败。看起来它会导致不平衡的括号出现问题,但实际上效果很好。我承认它看起来确实很尴尬。
  • 重启后还能继续吗?
  • @AshishK - 当然不是。没有进程在重新启动后存活。可能有一个逻辑进程在重新启动时恢复(重新启动),但这将是一个继续逻辑进程的新物理进程。但这与这里无关。
【解决方案3】:

您可以像这样利用CSCRIPT.EXEWSCRIPT.EXE(我相信自Windows 95 以来的每个版本中都存在):

echo msgbox "Hey! Here is a message!" > %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
del %tmp%\tmp.vbs

echo msgbox "Hey! Here is a message!" > %tmp%\tmp.vbs
wscript %tmp%\tmp.vbs
del %tmp%\tmp.vbs

您还可以选择更可自定义的PopUp 命令。此示例在超时之前为您提供了 10 秒的窗口来单击“确定”:

echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "You have 10 seconds to Click 'OK'." ,10 ,"Click OK", 0)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
if %errorlevel%==1 (
  echo You Clicked OK
) else (
  echo The Message timed out.
)
del %tmp%\tmp.vbs

在上述情况下,cscriptwscript 的行为相同。当从批处理文件调用时,bot cscriptwscript 将暂停批处理文件,直到它们完成脚本,然后允许文件继续。

从命令提示符手动调用时,cscript 在完成之前不会将控制权返回给命令提示符,而wscript 将创建一个单独的线程来执行它的脚本,甚至将控制权返回给命令提示符在它的脚本完成之前。

此线程中讨论的其他方法不会导致批处理文件的执行在等待消息被单击时暂停。您的选择将取决于您的需求。

注意:使用此方法,可以使用多个按钮和图标配置来覆盖用户的各种yes/no/cancel/abort/retry查询:https://technet.microsoft.com/en-us/library/ee156593.aspx

【讨论】:

    【解决方案4】:

    其他几种方式(在所有这些方式中,脚本都在等待按钮按下,这与 msg.exe 不同)。

    1) 最极客和最黑客 - 它使用 IEXPRESS 创建小型 exe,该 exe 将创建一个带有单个按钮 (it can create two more types of pop-up messages) 的弹出窗口。适用于 XP 及更高版本的每个窗口:

    ;@echo off
    ;setlocal
    
    ;set ppopup_executable=popupe.exe
    ;set "message2=%~1"
    ;
    ;del /q /f %tmp%\yes >nul 2>&1
    ;
    ;copy /y "%~f0" "%temp%\popup.sed" >nul 2>&1
    
    ;(echo(FinishMessage=%message2%)>>"%temp%\popup.sed";
    ;(echo(TargetName=%cd%\%ppopup_executable%)>>"%temp%\popup.sed";
    ;(echo(FriendlyName=%message1_title%)>>"%temp%\popup.sed"
    ;
    ;iexpress /n /q /m %temp%\popup.sed
    ;%ppopup_executable%
    ;rem del /q /f %ppopup_executable% >nul 2>&1
    
    ;pause
    
    ;endlocal
    ;exit /b 0
    
    
    [Version]
    Class=IEXPRESS
    SEDVersion=3
    [Options]
    PackagePurpose=InstallApp
    ShowInstallProgramWindow=1
    HideExtractAnimation=1
    UseLongFileName=0
    InsideCompressed=0
    CAB_FixedSize=0
    CAB_ResvCodeSigning=0
    RebootMode=N
    InstallPrompt=%InstallPrompt%
    DisplayLicense=%DisplayLicense%
    FinishMessage=%FinishMessage%
    TargetName=%TargetName%
    FriendlyName=%FriendlyName%
    AppLaunched=%AppLaunched%
    PostInstallCmd=%PostInstallCmd%
    AdminQuietInstCmd=%AdminQuietInstCmd%
    UserQuietInstCmd=%UserQuietInstCmd%
    SourceFiles=SourceFiles
    [SourceFiles]
    SourceFiles0=C:\Windows\System32\
    [SourceFiles0]
    %FILE0%=
    
    
    [Strings]
    AppLaunched=subst.exe
    PostInstallCmd=<None>
    AdminQuietInstCmd=
    UserQuietInstCmd=
    FILE0="subst.exe"
    DisplayLicense=
    InstallPrompt=
    ;
    

    示例用法(如果您将脚本保存为expPopup.bat):

    call expPopup.bat "my Message"
    

    2) 使用MSHTA。也适用于 XP 及更高版本的每台 Windows 机器(尽管 OP 不想要“外部”语言,但此处的 jsvascript 已最小化)。应保存为.bat

    @if (true == false) @end /*!
    @echo off
    mshta "about:<script src='file://%~f0'></script><script>close()</script>" %*
    goto :EOF */
    
    alert("Hello, world!");
    

    或一行:

    mshta "about:<script>alert('Hello, world!');close()</script>"
    

    mshta "javascript:alert('message');close()"
    

    mshta.exe vbscript:Execute("msgbox ""message"",0,""title"":close")
    

    3) 这里是参数化的.bat/jscript hybrid(应该保存为bat)。尽管有OP请求,但它再次使用jscript,但由于它是bat,因此可以称为bat文件不用担心。它使用POPUP,它比更流行的MSGBOX 允许更多的控制。它使用WSH,但不像上面的例子那样使用MSHTA。

     @if (@x)==(@y) @end /***** jscript comment ******
         @echo off
    
         cscript //E:JScript //nologo "%~f0" "%~nx0" %*
         exit /b 0
    
     @if (@x)==(@y) @end ******  end comment *********/
     
    
    var wshShell = WScript.CreateObject("WScript.Shell");
    var args=WScript.Arguments;
    var title=args.Item(0);
    
    var timeout=-1;
    var pressed_message="button pressed";
    var timeout_message="timedout";
    var message="";
    
    function printHelp() {
        WScript.Echo(title + "[-title Title] [-timeout m] [-tom \"Time-out message\"] [-pbm \"Pressed button message\"]  [-message \"pop-up message\"]");
    }
    
    if (WScript.Arguments.Length==1){
        runPopup();
        WScript.Quit(0);
    }
    
    if (args.Item(1).toLowerCase() == "-help" ||  args.Item(1).toLowerCase() == "-h" ) {
        printHelp();
        WScript.Quit(0);
    }
    
    if (WScript.Arguments.Length % 2 == 0 ) {
        WScript.Echo("Illegal arguments ");
        printHelp();
        WScript.Quit(1);
    }
    
    for (var arg = 1 ; arg<args.Length;arg=arg+2) {
            
        if (args.Item(arg).toLowerCase() == "-title") {
            title = args.Item(arg+1);
        }
        
        if (args.Item(arg).toLowerCase() == "-timeout") {
            timeout = parseInt(args.Item(arg+1));
            if (isNaN(timeout)) {
                timeout=-1;
            }
        }
        
        if (args.Item(arg).toLowerCase() == "-tom") {
            timeout_message = args.Item(arg+1);
        }
        
        if (args.Item(arg).toLowerCase() == "-pbm") {
            pressed_message = args.Item(arg+1);
        }
        
        if (args.Item(arg).toLowerCase() == "-message") {
            message = args.Item(arg+1);
        }
    }
     
    function runPopup(){
        var btn = wshShell.Popup(message, timeout, title, 0x0 + 0x10);
         
        switch(btn) {
            // button pressed.
            case 1:
                WScript.Echo(pressed_message);
                break;
    
            // Timed out.
            case -1:
               WScript.Echo(timeout_message);
               break;
        }
    }
    
    runPopup();
    

    示例用法(按下是按钮将等待 10 秒):

    jsPopup.bat -title CoolTitile -t 10 -tom "time out" -pbm "press the button please" -message "love and peace"
    

    4)和一个jscript.net/.bat混合(应该保存为.bat)。这次它使用.NET并编译一个可以删除的小.exe文件:

    @if (@X)==(@Y) @end /****** silent jscript comment ******
    
    @echo off
    ::::::::::::::::::::::::::::::::::::
    :::       compile the script    ::::
    ::::::::::::::::::::::::::::::::::::
    setlocal
    
    
    ::if exist "%~n0.exe" goto :skip_compilation
    
    :: searching the latest installed .net framework
    for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
        if exist "%%v\jsc.exe" (
            rem :: the javascript.net compiler
            set "jsc=%%~dpsnfxv\jsc.exe"
            goto :break_loop
        )
    )
    echo jsc.exe not found && exit /b 0
    :break_loop
    
    
    
    call %jsc% /nologo /out:"%~n0.exe" "%~f0" 
    ::::::::::::::::::::::::::::::::::::
    :::       end of compilation    ::::
    ::::::::::::::::::::::::::::::::::::
    :skip_compilation
    
    ::
    ::::::::::
    "%~n0.exe" %*
    ::::::::
    ::
    endlocal
    exit /b 0
    
    ****** end of jscript comment ******/
    
    import System;
    import System.WIndows;
    import System.Windows.Forms
    
    var arguments:String[] = Environment.GetCommandLineArgs();
    MessageBox.Show(arguments[1],arguments[0]);
    

    示例用法:

    netPopUp.bat "Roger That"
    

    5),最后一次调用 powershell 创建一个弹出窗口(如果安装了 powershell,可以从命令行或批处理调用):

    powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show("""Hello World""", """My PopUp Message Box""")
    

    6) 虽然msg 解决方案已作为答案发布,但这里有一个更好的使用方法:

    msg * /self /w "hello world"
    

    /self 是一个未记录的开关,它将强制 msg 仅将消息发送给当前用户。

    【讨论】:

      【解决方案5】:

      msg * 你好世界

      为我工作..

      【讨论】:

        【解决方案6】:

        所以,我提出 cmdmsg.bat。

        代码是:

        @echo off
        echo WScript.Quit MsgBox(%1, vbYesNo) > #.vbs
        cscript //nologo #.vbs
        echo. >%ERRORLEVEL%.cm
        del #.vbs
        exit /b
        

        还有一个示例文件:

        @echo off 
        cls 
        call cmdmsg "hi select yes or no"
        
        if exist "6.cm" call :yes
        if exist "7.cm" call :no
        
        :yes
        cls
        if exist "6.cm" del 6.cm
        if exist "7.cm" del 7.cm
        echo.
        echo you selected yes
        echo.
        pause >nul
        exit /b
        
        :no
        cls
        if exist "6.cm" del 6.cm
        if exist "7.cm" del 7.cm
        echo.
        echo aw man, you selected no
        echo.
        pause >nul
        exit /b
        

        【讨论】:

          【解决方案7】:

          您最好使用NET SEND,如Rob van der Woude's site 中所述。

          否则,您将需要使用外部脚本程序。批处理文件实际上是为了通过ECHO发送消息。

          【讨论】:

            【解决方案8】:

            这很简单,因为我已经创建了几行代码来为你做这件事

            所以设置一个变量为 msg 然后使用这个代码。它会在 VBS 消息框中弹出。

            代码:

            @echo off
            echo %msg% >vbs.txt
            copy vbs.txt vbs.vbs
            del vbs.txt
            start vbs.vbs
            timeout /t 1
            del vbs.vbs
            cls
            

            这只是我想出来的,它应该可以满足您的大部分消息需求,并且它也适用于 Spaces,这与某些批处理脚本不同

            【讨论】:

              【解决方案9】:

              发消息很容易,方法如下:

              首先打开记事本并输入:

              msg "Message",0,"Title"
              

              并将其保存为 Message.vbs。

              现在在您的批处理文件类型中:

              Message.vbs %*
              

              【讨论】:

                【解决方案10】:

                msg * message goes here

                该方法非常简单易行,我相信它应该适用于任何批处理文件。此方法的唯一“缺点”是它一次只能显示 1 条消息,如果有多条消息,它将根据您将它们放入代码中的顺序逐条显示。还要确保批处理文件中有不同的循环或连续运算符,否则它将自动关闭并且只会出现此消息。如果你需要一个“安静”的后台循环操作符,这里有一个:

                pause &gt;nul

                它应该保持运行,但在按下按钮后它会关闭。

                另外为了让所有命令在运行时保持“安静”,所以它们只是运行而不显示它们被输入到文件中,只需将以下行放在批处理文件的开头:

                @echo off

                我希望所有这些提示对您有所帮助!

                【讨论】:

                  【解决方案11】:

                  我根据这里和其他帖子中的好答案编写了一个脚本

                  您可以设置标题超时甚至睡眠来安排它为后者和 \n 为新行

                  您还可以将按键返回到变量 (%pop.key%)。

                  Here 是我的代码

                  【讨论】:

                  • 问题明确指出without using VBScript or KiXtart or any other external scripting/programming language
                  • 我明白了,但其他 10 个答案中只有 4 个在使用 msgNet send(他不能使用)、echo,所以我只是将最佳答案选择到一个文件中, & 毕竟它不是第三方应用 & 一旦你把它放在一个路径文件夹中你不需要知道任何脚本/编程语言, 你只需输入 popup & 你的消息
                  猜你喜欢
                  • 2015-07-23
                  • 2014-04-10
                  • 2022-12-06
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-07-30
                  • 1970-01-01
                  • 2011-06-15
                  • 2015-11-16
                  相关资源
                  最近更新 更多