When users click the close (X) button of a batch file window, I want it to show a confirmation dialog asking "Are you sure want to close this batch file?" with Yes/No options. How can I do this?

  • 1
    So if you already have a batch that does what you want to make a batch do, the answer is right there in the batch that you have.
    – EBGreen
    May 18 '15 at 16:11
  • @EBGreen I meant when the user clicks the "X" button
    – Nuach
    May 18 '15 at 16:12
  • See pause and choice for the CMD prompt. May 18 '15 at 16:12
  • @DrMoishePippik You when the user presses the red "X" button on the top right corner of the batch file, I want the batch file to comfirm the closing of the batch file.
    – Nuach
    May 18 '15 at 16:16
  • 2

There are other great options than using a batch file for your purpose, but since you want it that way, here is the code
@echo off Call :YesNoBox "Are you sure you want to do that?" if "%YesNo%"=="7" ( Call :MessageBox "You answered NO" "Heading" exit /b )
Code for invoking the message box

exit /b :YesNoBox REM returns 6 = Yes, 7 = No. Type=4 = Yes/No set YesNo= set MsgType=4 set heading=%~2 set message=%~1 echo wscript.echo msgbox(WScript.Arguments(0),%MsgType%,WScript.Arguments(1)) >"%temp%\input.vbs" for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set YesNo=%%a exit /b
The above code is for programming the YesNo Options

:MessageBox set heading=%~2 set message=%~1 echo msgbox WScript.Arguments(0),0,WScript.Arguments(1) >"%temp%\input.vbs" cscript //nologo "%temp%\input.vbs" "%message%" "%heading%" exit /b
The above code is for setting up and programming the Message Window

Hope it helps, but if you further want to refer for your purpose you should visit these links : 1st Link, 2nd Link


  • How will this help if the user closes the cmd window by clicking the 'X' button?
    – Karan
    May 18 '15 at 19:31
  • This is definitely not the idea in the code.. The code is solely aimed at creating a window which prompts the user for the option for closing the batch operation
    – mustangDC
    May 18 '15 at 19:35
  • I know, but that's not the OP's requirement so this clearly won't help.
    – Karan
    May 18 '15 at 19:35
  • Ya, you are right. Thats why I have provided a similar code not the exact working one and backed them up with references so that if the links die the code will be there for reference. Although I am working on the exact solution, will be posting it soon.
    – mustangDC
    May 18 '15 at 19:51
  • mistakenly posted as an aswer from the android app. Will delete it soon Sorry
    – mustangDC
    May 18 '15 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.