我写了一个正在执行 net use
的脚本在开头和 net use /DELETE
在末尾。
但如果用户决定按 Ctrl + C 并退出脚本,我需要执行 net use /DELETE
.
那可能吗?我在谷歌上找不到任何东西。
最佳答案
当然,只需在新的 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 session 关闭后,命令将始终触发。它可能由于正常运行、用户按下 Ctrl-C 或 fatal error 而关闭 - 最终 net use \delete
还是会开火。
关于batch-file - 如果用户在退出前按 Ctrl+C 执行命令,则批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27130050/