Batch check if mapped network drive exists
Asked Answered
F

6

42

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.

Faus answered 2/12, 2014 at 10:18 Comment(2)
Start with for /F "tokens=1,2*" %G in ('net use^|Find "\\"^|Find /I "Z:"') do @echo %H %I to see an example. (Using that in a batch script, double the % sign, use %%G, %%H etc.) Learn more .... Then you could replace that simple @echo %%H %%I with multiple commands in FOR loop. Feel free to update your question with actual achievements and ask more in case you crane at something.Osteophyte
Why not just use the next available drive letter intead of destroying the users current drive mapping: net use * \\path. or you can use the PUSHD command which will map the next available drive letter and immediately do a change directory to that path: PUSHD \\pathGrearson
L
63

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
Lipfert answered 2/12, 2014 at 10:42 Comment(3)
Also useful: if not exist z:\ (net use z: \\my\unc\path)Nucleate
This only answers part of the question and does not answer the full question of re-mapping the drive to a different letter.Klemens
Note; If you have a disconnected (but still remembered) mapped drive, it doesn't show up when accessed this way. I haven't found a workaround / ended up just removing the if condition and deleting the mapped drive regardless of whether or not one existed)Barbwire
C
5

I use the following scriptbit to unmap all drives:

:: First unmap all network drives
FOR /F "tokens=1,2,3" %%G IN ('net use^| Find "\\"') DO (
  ECHO.Unmapping %%I from drive letter %%H
  NET USE %%H /D > NUL
)

What does it do? Let's split this up

FOR /F "tokens=1,2,3" %%G IN () DO ()--> will iterate over the set defined between () and the first, second and third word (any whitespace is used as separator) will become available as %%G, %%H and %%I respectively.

('...') will run a command ... and pass the result(s) to the FOR loop

net use will output something like this:

New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           M:        \\diskstation\music       Microsoft Windows Network
OK           P:        \\diskstation\home        Microsoft Windows Network
OK           V:        \\diskstation\video       Microsoft Windows Network
OK           X:        \\diskstation\photo       Microsoft Windows Network
The command completed successfully.

This output is then piped to Find.exe (the windows equivalent to grep) which looks for "\\".

The pipe symbol | is escaped using ^ so cmd does not execute it directly. The output will then be:

OK           M:        \\diskstation\music       Microsoft Windows Network
OK           P:        \\diskstation\home        Microsoft Windows Network
OK           V:        \\diskstation\video       Microsoft Windows Network
OK           X:        \\diskstation\photo       Microsoft Windows Network

NET USE /D will delete the drive mapping for the selected mapping

Converge answered 12/5, 2015 at 5:49 Comment(2)
First, this does nothing to answer the posters question. Second, this breaks if the remote UNC path is long and causes the network column to display on a second line. This script is overkill for deleting all drive mappings. A simpler way to remove all drive mappings is to use net use * /d /y.Klemens
Third, it'll break, if Status is not OK and consists of multiple words e. g. Not available. Then tokens will catch the wrong parts.Flannelette
S
4

Another possible way could be:

net use Z:
if %errorlevel% EQU 0 net use Z: /delete
net use Z: \\path
Sexagenary answered 8/4, 2015 at 16:48 Comment(1)
I like this because you can use it with just a share too, e.g. net use \\some\shareCockcroft
P
3

My requirements were slightly different than the OP's: I needed to map a network drive, always to the same path, but only if it didn't exist already

if not exist z:\ (
    net use z: \\ComputerName\ShareName
)
Peltast answered 14/2, 2020 at 18:22 Comment(1)
z: may be in Disconnected or Unavailable state, in this case remapping may fail.Franklyn
K
2

To answer the original question, you can check if Z is already mapped and then re-map it to a different drive letter (e.g. y:), then map z to the new path.

if exist z:\ (
    for /F "tokens=1,2*" %%G in ('net use^|Find "\\"^|Find /I "z:"')  do ( net use y: %%H )
    net use z: /delete
)
net use z: \\path
Klemens answered 15/11, 2016 at 14:28 Comment(1)
As already commented below similar answers, `if exist z:` does not check if a drive letter is just mapped. Instead, it returns true if a drive letter is mapped AND if the destination is available. So with your solution, you cannot delete a mapped drive, that is currently unavailable.Flannelette
L
1

In batch you can write code like this:

@echo off
net use V: >nul 2>&1
if %errorlevel% equ 0 goto unmap

if exist V:\ does not work on Windows 10, also net use displays additional information, I have now suppressed it using >nul 2>&1.

Leakage answered 15/8, 2018 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.