Reputation: 25138
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.
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.
Upvotes: 0
Views: 12355
Reputation: 356
TL;DR:
Following will unmount drive I
without printing anything on console and keeping errorlevel
at 0.
net use I: /delete /Y > NUL 2>&1 || dir > NUL
Detail:
Though the accepted answer is correct but it raises the errorlevel
if the drive was not mounted. Since I only wanted to check and unmount existing drive without changing errorlevel, I came up with the command above.
net use I: /delete /Y
tries to unmount drive I
right away whereas > NUL 2>&1
redirects the output to NUL
device (hiding it from console).
If unmount fails (mount not detected), conditional execution ||
starts where dir > NUL
prints current directory content to NUL
device (again printing nothing on console). This resets the errorlevel to 0.
Final result is that nothing gets printed on the console and errorlevel stays at 0.
Source: https://ss64.com/nt/net-use.html
Disconnect from a share and close all resources (undocumented)
NET USE [driveletter:] /DELETE /Y
Upvotes: 4
Reputation: 70943
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.
Upvotes: 5