JSI Tip 3491. How do I extract the 'server' and 'share name' from a drive letter mapped by 'net use'?

Jerold Schulman

March 19, 2001

1 Min Read
ITPro Today logo in a gray background | ITPro Today


If you need to determine the 'server name' and 'share name' of a drive letter that was mapped with the net use command, you can call drivemap in your batch file. Drivemap.bat returns the server component of the mapped drive in the server environment variable and returns the share name in the the share environment variable. If the drive is not mapped, or you do not provide a valid drive parameter, both environment variables are set to None. Drivemap.bat contains:

@echo offsetlocalset server=Noneset share=Noneif {%1}
{} goto endREM Clean up drive letter parameterset dl=%1set dl=%dl::=%set dl=%dl:"=%set dl=%dl:=%set dl=%dl%:if not exist %dl%*.* goto endfor /f "Skip=1 Tokens=3* Delims= " %%i in ('net use %dl%^|findstr "Remote name"') do set server=%%i&set share=%%j:endendlocal&set server=%server%&set share=%share%

If your CMD parser is pipe ( | ) challenged, you can use the following script:

@echo offsetlocalset server=Noneset share=Noneif {%1}{} goto finREM Clean up drive letter parameterset dl=%1set dl=%dl::=%set dl=%dl:"=%set dl=%dl:=%set dl=%dl%:if not exist %dl%*.* goto finfor /f "Skip=1 Tokens=1-3* Delims= " %%i in ('net use %dl%') do call :parse %%i %%j %%k "%%l":finendlocal&set server=%server%&set share=%share%goto end:parseif not "%1"=="Remote" goto endset server=%3set share=%4set share=%share:"=%:end



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like