batch script if user press Ctrl+C do a command bef

2019-01-26 09:51发布

问题:

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.

回答1:

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.



回答2:

My idea is similar to dbenham's. Took me forever to figure out how to minimize the current console window though. I banged my head against the wall trying to get the cmd window not to ignore an Alt+Space keypress using Wscript.Shell's .SendKeys method. Finally I turned to PowerShell to handle minimizing and restoring the working window.

The advantage to this over dbenham's is that you'll inevitably have some rectal-cranially inverted user who gets bored with the running of your script and terminates it with the red X. dbenham's won't catch that, but mine should.

@echo off
setlocal

if "%~1" neq "wrapped" (

    rem :: Map network drive
    net use y: \\computername\c$ >NUL

    rem :: minimize this console
    powershell -windowstyle minimized -command ""

    rem :: relaunch self with "wrapped" argument and wait for completion
    start /wait "" cmd /c %~f0 wrapped

    rem :: After script completes or user interrupts, remove drive mapping and restore window
    net use y: /delete >NUL
    powershell -windowstyle normal -command ""

    goto :EOF

)

:: Main script goes here.
:loop
cls
echo Simulating script execution...
ping -n 2 0.0.0.0 >NUL
goto loop


回答3:

There is a very simple solution. Just write:

ping -l www."site".com -t 65500>nul

before your net use /delete command. The only way to break that command is to press ctrl+c. By example:

net use "arguments"
ping -l www.google.com -t 65500>nul
net use /delete

This is a good way to detect ctrl+c, but beware of the site address you write, because it risks to make that site crash. You should, by consequence, write the address of an unusual site, or of a site of your own(Attention: the site must be existing), like a blog or something like that.



回答4:

I don't think this is possible. At the beginning of your script, you can use:

net use /delete 2>nul
net use g: \\server\sharename /persistent:no

Or you could try pushd instead of net use...

If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter.  Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.

This way, you will always have mapped drive correctly set.



回答5:

You need to use traps to do this Traps

trap ctrl_c INT

function ctrl_c() {
        echo "Trapped"
}