Batch check if mapped network drive exists

Alex Brodov picture Alex Brodov · Dec 2, 2014 · Viewed 93.1k times · Source

I'm currently writing a script that should map a network drive to the letter Z , i'm using the command net use z: \\path , the thing is that if the user is already using this letter i won't be able to map it, is there any way to check the existense of this drive (z) and if it exists to unmount it and mount it to a different letter and still use the z drive which i need for my script, this is a part of an installation, and it should be on Z drive.

Answer

MichaelS picture MichaelS · Dec 2, 2014

You can check whether the drive is mounted by IF EXIST Z:\. This should work:

if exist z:\ (
    net use z: /delete
)
net use z: \\path