Skip navigation

JSI Tip 4161. How can I run a script on client computers that requires a mapped drive if they don't have any available drive letters?

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 <DriveLetter>

Remap.bat returns the drive environment variable set to the <DriveLetter> parameter you used, if the <DriveLetter> is mapped. If the <DriveLetter> 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

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.