如果用户在退出前按 Ctrl+C 执行命令,则批处理脚本 [英] batch script if user press Ctrl+C do a command before exiting
问题描述
我写了一个脚本,开头是net use
,结尾是net use/DELETE
.
但如果用户决定按 Ctrl + C 并退出脚本,我需要执行 net use/DELETE
.>
这可能吗?我在谷歌上找不到任何东西.
当然,只需在新的 CMD 会话中执行大部分脚本:
@echo offif "%~1" neq "_start_" (净使用...cmd/c "%~f0" _start_ %*净使用/删除...退出/b)移位/1REM 脚本的其余部分放在这里
只要您的控制台窗口保持打开状态,net use/delete
命令就会在内部 cmd 会话关闭后始终触发.它可能会因为正常运行、用户按下 Ctrl-C 或致命错误而关闭 - 最后的 net use delete
仍会触发.
I wrote a script which is doing net use
at the beginning and net use /DELETE
at the end.
But if user decides to press Ctrl + C and exits the script, I need to do a net use /DELETE
.
Is that possible? I can't find anything on google.
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
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屋!