Script package, if the user press Ctrl + C, execute the command before exiting

I wrote a script that does net use at the beginning and net use /DELETE at the end.

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

Is it possible? I can’t find anything on Google.

+7
cmd batch-file exit
source share
5 answers

Of course, just 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 the console window remains open, the net use /delete command always runs after closing the internal cmd session. It may close due to a normal start, the user presses Ctrl-C or a fatal error - the final net use \delete still works.

+13
source share

My idea is similar to dbenham's. It took forever to figure out how to minimize the current console window. I hit my head against a wall, trying to make the cmd window not ignore pressing Alt+Space using the Wscript.Shell .SendKeys method. Finally, I turned to PowerShell to handle minimizing and restoring the working window.

The advantage of this over dbenham is that you will inevitably have some kind of rectally-cranially inverted user who gets bored with running your script and terminates it with red X. dbenham will not catch this, but mine follows.

 @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
source share

There is a very simple solution. Just write:

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

before the net use / delete command. The only way to break this command is to press ctrl + c. 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 address of the site you are writing, because this may cause this site to crash. As a result, you must write the address of an unusual site or your own site (Note: the site must be already existing), for example, on a blog or something like that.

+1
source share

I do not think that's possible. At the beginning of your script, you can use:

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

Or you can 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. 

Thus, you will always be correctly configured correctly configured drive.

0
source share

For this you need to use traps Traps

 trap ctrl_c INT function ctrl_c() { echo "Trapped" } 
-4
source share

All Articles