如果用户在退出前按Ctrl + C则执行批处理脚本 [英] batch script if user press Ctrl+C do a command before exiting

查看:578
本文介绍了如果用户在退出前按Ctrl + C则执行批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,该脚本的开头是net use,结尾是net use /DELETE.

I wrote a script which is doing net use at the beginning and net use /DELETE at the end.

但是,如果用户决定按 Ctrl + C 并退出脚本,则需要执行net use /DELETE.

But if user decides to press Ctrl + C and exits the script, I need to do a net use /DELETE.

有可能吗?我在Google上找不到任何东西.

Is that possible? I can't find anything on google.

推荐答案

当然,只需在新的CMD会话中执行大部分脚本即可:

Sure, simply execute most of your script in a new CMD session:

@echo off
if "%~1" neq "_start_" (
  net use ...
  cmd /c "%~f0" _start_ %*
  net use /delete ...
  exit /b
)
shift /1
REM rest of script goes here

只要您的控制台窗口保持打开状态,net use /delete命令将始终在内部cmd会话关闭后触发.它可能由于正常运行,用户按Ctrl-C或致命错误而关闭,最终的net use \delete仍会触发.

As long as your console window remains open, the net use /delete command will always fire after the inner cmd session closes. It could close because of normal run, user presses Ctrl-C, or fatal error - the final net use \delete will still fire.

这篇关于如果用户在退出前按Ctrl + C则执行批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆