If your script requires a mapped drive, you would normally use tip 1002 ยป Using a temporary drive letter is a batch.

If the client doesn't have any available drive letters, or your script expects a specific drive letter, which may be improperly mapped, you can use remap.bat to determine the current mapping of the drive letter you want to use. The syntax is:


Remap.bat returns the drive environment variable set to the parameter you used, if the is mapped. If the is mapped, it also returns the map environment variable with the current mapping.

Remap.bat contains:

@echo off
If "%1" EQU "" goto syntax
set param=%1
for /f "Skip=6 Tokens=*" %%i in ('net use') do set line="%%i"&call :parse
endlocal&set drive=%drive%&set map=%map%
goto :EOF
@echo Syntax: remap DriveLetter
goto :EOF
If /i NOT "%line:~14,1%" EQU "%param%" goto :EOF
set drive=%line:~14,1%
set map=%line:~24,99%
set map=%map:Microsoft Windows Network"=%#
set wrk=%map%
set wrk=%wrk: #=#%
if "%wrk%" EQU "%map%" goto final
set map=%wrk%
goto :loop
set map=%map:#=%

If you want to use drive letter m to run your script:

@echo off
remap m
if /i NOT "%drive%" EQU "m" goto yourM
net use M: /delete
net use M: \\YourServer\YourShare
net use M: /delete
if /i NOT "%drive%" EQU "m" goto :EOF
net use M: %map% /persistent:YES