简体   繁体   中英

Determine what drive letter a network share is mapped to using batch file

Issue: I am having trouble identifying the drive letter that a network share is mapped to using a batch file.

Background: We map and use about a dozen network shared drives at work, and the batch file currently maps the drive to the "usual" drive letter. But I want to know if it is mapped to any letter (and if so, what that letter is) because we have over 200 PCs that use these mapped drives, and the letter is often different, so mapping it again to the "usual" letter results in duplicated mapped network drives.

I can see that the network share is mapped to a drive in net use , and it is accessible using both UNC path or the mapped drive letter, so not sure why I can't get the drive letter.

I located the following 3 answers for this issue, but for some reason, none of them are working for me. The URLs, code, and results obtained from each are below. Any help is greatly appreciated!


Mapping a network drive without hardcoding a drive letter in a batch file

for /f "tokens=2" %%i in ('net use ^| find "\\NetworkShare1\Share1"') do set netdrive=%%i

Result: \NetworkShare1\Share1 has been mapped


Return users mapped drive letter from network drive remote

FOR /F "tokens=2" %%D IN ('net use ^| find ":" ^| find /I "\\NetworkShare1\Share1"') DO ECHO Drive letter is %%D

Result: Drive letter is \NetworkShare1\Share1


How to get drive letter of mapped network drive

for /f "tokens=2,3" %%i in ('net use') do if '%%j=='\\NetworkShare1\Share1 set drive=%%i
echo %drive%

Result: Echo is off.

On my windows 10 machine:

@echo off
set UNC=\\yourserver\yourshare
set drv=

for /f "delims=: " %%a in ('net use ^| find /i "%UNC%"') do set drv=%%a
if defined drv (
   echo "%UNC%" is mapped to %drv%:
   ) else (
      echo "%UNC%" is not mapped to anything.
)
echo/
pause

Note there is a space after the :

Also I should note that if the share is mapped to multiple drive letters this will only return the "last" drive letter mapped. I believe it is alphabetical.

You know what -- I'm going to suggest a change. This one will list all drives mapped to the path, and it will include partial matches. In other words, if you search for \serv1\data it will also match \serv1\data\subfolder. But then it will also match \serv1\datagram. This might be useful or a problem.

@echo off
set "UNC=\\yourserver\yourshare"
set UNCfound=no

for /f "tokens=1,2 delims=: " %%a in ('net use ^| find /i "%UNC%"') do (
   echo "%%~b" is mapped to %%a:
   set UNCfound=yes
)
if "%UNCfound%"=="no" echo "%UNC%" is not mapped to anything.

echo/
pause

From my testing, if you add a space in the find expression, you can do an exact match:

@echo off
set "UNC=\\yourserver\yourshare"
set UNCfound=no

for /f "tokens=1,2 delims=: " %%a in ('net use ^| find /i "%UNC% "') do (
   echo "%%~b" is mapped to %%a:
   set UNCfound=yes
)
if "%UNCfound%"=="no" echo "%UNC%" is not mapped to anything.

echo/
pause

Here is an approach featuring the wmic command , using the Win32_LogicalDisk class :

@echo off
set "UNC=\\ATMEDFS01\workgroups\R&D\Systems"
set "DRV="

for /F "delims=" %%I in ('
    wmic LogicalDisk where "DriveType=4 and ProviderName='%UNC:\=\\%'" get DeviceID /VALUE 2^> nul
') do for /F "tokens=1* delims==" %%J in ("%%I") do set "DRV=%%K"

if defined DRV (
    echo The network share "%UNC%" is mapped to drive %DRV%.
) else (
    echo The network share "%UNC%" is NOT mapped to a drive.
)

In the where clause of the wmic command line, every \ needs to be escaped by doubling it; that is what the sub-string substitution expression %UNC:\=\\% is for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM