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.