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.
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
if not exist z:\ (net use z: \\my\unc\path)
–
Nucleate 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
Not available
. Then tokens
will catch the wrong parts. –
Flannelette Another possible way could be:
net use Z:
if %errorlevel% EQU 0 net use Z: /delete
net use Z: \\path
net use \\some\share
–
Cockcroft 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
)
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
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
.
© 2022 - 2024 — McMap. All rights reserved.
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. – Osteophytenet 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 \\path
– Grearson