batch-filenet-use

net use delete in batch if drive exists


i would like setup script which can connect network drive like this:

net use v: /delete || EXIT /B 1
net use v: "\\vmware-hostservername\share Folder" 

I need to cover two scenario.

  1. Run test after restarting of PC (no disc v: available)
  2. run test repeatedly. Disc v: already exists and need to be deleted and mapped again.

I cannot put it to the window startup, because there are timeouts for disconnecting and the mapping need to done before running the script, so it ensures the drive will be available (and not disconnected).

The scrip provided does not work after restarting PC because v: does not exists. It need some modification like

if exists V: net use v: /delete || EXIT /B 1

I am not too familiar with batch syntax of above psedocode.


Solution

  • Just check if the mapping exists

    net use v: && (net use v: /delete /Y || exit /b 1)
    

    It executes net use v: to show the information for the drive. Then it uses conditional execution operator &&, that is, execute the next command if the previous one was sucessful.

    If the drive exists, then it just tries to remove the mapping and in this case the conditional execution operator || (execute the next command if the previous one failed) is used.