执行批处理code时退出用户点击

执行批处理code时退出用户点击

本文介绍了执行批处理code时退出用户点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些code测试,我无意中发现一个问题,我无法找到或修复。我的问题是:

I am working on some code testing, and I stumbled on a problem I can't find or fix. My problem is:

如果用户不小心关闭cmd窗口,我想它实际上是关闭之前执行一批code。 例如:

If a user accidentally closes the cmd window, I'd like to execute a batch code before it actually closes. For example:

我运行脚本A.bat。当用户想要退出,我希望它删除我B.bat,然后关闭窗口。
结果
这是怎样的code可能看起来像:结果

I run script A.bat . When a user wants to exit, I want it to delete my B.bat and then close the window.
This is how the code may look like:

@ECHO OFF
echo Welcome to A.bat
del B.bat (when user exits the window)

我找不到它在谷歌和论坛,所以我想也许你们可以帮助我。由于提前,尼尔斯

I couldn't find it on google and forums, so I thought maybe you guys could help me out. Thanks in advance, Niels

推荐答案

这对我的作品:

@ECHO OFF
if "%1" equ "Restarted" goto %1
start "" /WAIT /B "%~F0" Restarted
del B.bat
goto :EOF

:Restarted

echo Welcome to A.bat
echo/
echo Press any key to end this program and delete B.bat file
echo (or just close this window via exit button)
pause
exit

修改补充几点说明

启动命令重新启动一个新的CMD.EXE会话相同的批处理文件; / B开关在同一个窗口中打开和/ WAIT开关使原始文件要等到新的一端。新的批处理文件必须以,以杀死新的CMD.EXE会话结束结束(因为它启动时使用/ K开关)。如果没有新的CMD.EXE会话正常结束,因为问题还是因为它是用红色的X取消了退出命令;在任何情况下,在原来的执行启动它的行后控制返回。

The start command restart the same Batch file in a new cmd.exe session; the /B switch open it in the same window and the /WAIT switch makes the original file to wait until the new one ends. The new Batch file must end with exit in order to kill the new cmd.exe session (because it was started with the /K switch). No matters if the new cmd.exe session ends normally because the exit command or because it was cancelled with the red X; in any case the control returns after the line that started it in the original execution.

这篇关于执行批处理code时退出用户点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:31