batch script if user press Ctrl+C do a command before exiting

后端 未结 5 808
闹比i
闹比i 2020-12-19 07:43

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 +

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 08:05

    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.

提交回复
热议问题