如何在关闭批处理文件时显示警告消息?

如何在关闭批处理文件时显示警告消息?

我有一个批处理文件。打开后,我想通过带有自定义消息的警告对话框警告用户关闭它。或者,如果上述方法不可行,至少在批处理文件中使用一些消息。

有没有什么办法可以做到呢?

答案1

或者,如果上述方法不可能,则批处理文件中至少包含一些消息。

一种方法是添加多REM行并包含echo on。然后您可以像下面这样放置横幅消息:

REM ************************
REM DO NOT CLOSE THIS WINDOW
REM *************************

参考这里更多信息REM

如果这没有按你预期的方式工作,那么请查看https://ss64.com/nt/rem.html

多行注释添加大型多行注释块测试的一种方法是使用纯文本和 goto 命令跳转注释后的执行:

@Echo OFF Goto :START Description can go here which can even include - | > characters

:START

上述技巧在括号表达式(例如循环)中间不起作用FOR... DO(...)。这是因为 Goto 命令不能在括号内使用 - 它会破坏其上下文。

也可以通过将注释文本放在括号内来处理这种情况,如下所示:

( Echo the lines below are commented Rem/||( some comment text that will work within brackets. The REM command always evaluates to TRUE so these lines will never be executed. ) )

答案2

在此处输入图片描述


@Goto %:^)

         ***   This text is your message to editor in your code   ***

%:^)
@echo off && mode 50,4 && title <nul && set "_yn=" <nul
setlocal & color 0A & title .\%~nx0 & >"%temp%\_vbs.vbs" ^
set /p "'=yn=msgbox("Hey!, Close %~nx0 Now!",4,"Warnning!!."):wsh.echo yn" <nul

%__AppDir__%cscript.exe "%temp%\_vbs.vbs" //nologo | find "6" >nul && set "_yn=y"
if "%_yn%"=="y" (endlocal && echo\Thank you, %~nx0 stopped by user!.. && goto :EOF)

set "_yn=" & rem :: if user answer is "No" do more below here ::

for %%i in (            "OK button only.  0.Simple Ok",
                 "OK and Cancel buttons.  1.Simple OK or Cancel",
            "Abort Retry Ignore buttons.  2.Simple Abort Retry or Ignore",
             "Yes No and Cancel buttons.  3.Yes No Cancel",
                        "Yes No buttons.  4.Yes or No",
                  "Retry Cancel buttons.  5.Retry Cancel",
                 "Critical Message icon. 16.Critical Error Retry Cancel",
                    "Warning Query icon. 32.Warnning and Simple Ok",
                  "Warning Message icon. 48.Warnning ans Simple Ok",
              "Information Message icon. 64.Warnning and Simple Ok",
           )do call %:^] "%%~i"  

%:^]
if "%~1" == "" %__AppDir__%timeout.exe -1 | <nul ^
echo\Press any key any time... & endlocal & goto :EOF

for /f tokens^=1-3delims^=. %%i in ('echo\%~1
')do echo\msgbox"%%~i",%%~j,"%%~k" >"%temp%\_vbs.vbs" 
%__AppDir__%wscript.exe "%temp%\_vbs.vbs" //nologo & exit /b

您可以在您的 bat 文件中使用 VBS 并要求您的用户是否关闭...

:: Create you VBS dialog box file: 
echo\Msgbox"You text msg", int_id,"Window Title" >"%temp%\_vbs.vbs"

run your VBS file:
%__AppDir__%cscript.exe "%temp%\_vbs.vbs" //nologo 

  • 获取用户点击的 cmd 变量:
:: set user answer click :: 
=yn=msgbox("Hey!, Close %~nx0 Now!",4,"Warnning!!."):wsh.echo yn"

:: set user answer click value/id Yes and No :: 
Yes == 6 
 No == 7

:: Command line run VBS |redirect output | find "answer_Number" ::
%__AppDir__%cscript.exe "%temp%\_vbs.vbs" //nologo |find "6" 

:: With operator && and set if find "6" ::
%__AppDir__%cscript.exe "%temp%\_vbs.vbs" //nologo |find "6" >nul && set "_yn=y"

:: VBS ID click returns:
        Ok = 1
    Cancel = 2
     Abort = 3
     Retry = 4
    Ignore = 5
       Yes = 6
        No = 7

在此处输入图片描述

相关内容