8

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.

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Nico
  • 251
  • 1
  • 3
  • 13

4 Answers4

16

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • That's a really nice idea ! Thanks @dbenham. I'll give this a try ASAP. I'm wondering why I didn't think for this kind of solution ! – Nico Nov 26 '14 at 07:25
  • 1
    EDIT - @Nico - I forgot the `SHIFT /1` so your script gets the original arguments - all fixed :-) – dbenham Nov 26 '14 at 07:31
3

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
rojo
  • 24,000
  • 5
  • 55
  • 101
  • 1
    I noticed that when pressing the red X, I get the question _Terminate batch job (Y/N)?_. This can be prevented by adding an extra indirection of start like: `start /wait /min "" cmd /c start /wait "" cmd /c %~f0 wrapped`. – eremmel Jun 15 '17 at 15:13
1

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.

Anonyme
  • 11
  • 1
0

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.

jftuga
  • 1,913
  • 5
  • 26
  • 49
  • As far as I understand (first time batch file), you start script by deleting the net use, and then doing a fresh new net use ? This means that if user press ctrl-C, net use still here, but if user re-run the script, net use is deleted before being added. Right ? – Nico Nov 25 '14 at 15:32
  • @Nico: This is correct and I think this is the only way. If they press Ctrl-C, the drive will still be mapped. I also edited the answer. – jftuga Nov 25 '14 at 15:37
  • 2
    @jftuga - It's actually rather simple :) See [my answer](http://stackoverflow.com/a/27131024/1012053) – dbenham Nov 25 '14 at 16:49
  • @dbenham - Brilliant. :-) – jftuga Nov 25 '14 at 18:21