JSI Tip 9015. How can I rename a mapped drive?


NOTE: See How can I make My Computer alter the Share Name on 'Computer Name (Comment Text) (Drive Letter:)' sequence that it shows for mapped drive letters on Windows XP SP2?

I have scripted MapName.bat to change name of a mapped drive.

Instead of seeing one of the following, I prefer to see (Drive Letter:) NewName:

Share Name on 'Comment Text (Computer Name) (Drive Letter:)
Share Name on 'Computer Name (Comment Text) (Drive Letter:)
(Drive Letter:) Share Name on 'Comment Text (Computer Name)
(Drive Letter:) Share Name on 'Computer Name (Comment Text)
The syntax for using MapName.bat is:

\[call\] MapName Drive NewName OK

Where:

Drive   is the mapped drive letter.
NewName is the name you wish to see. 
OK      is a call directed environment variable that will contain a Y if the rename was successful, or a N if the rename failed.

Sample Usage:

MapName L: "Jerry's Laptop" Status

will set the name of mapped drive letter L to Jerry's Laptop and set the Status environment variable to Y.

MapName.bat contains:

@echo off
if \{%3\}==\{\} @echo Syntax MapName Drive NewName OK&goto :EOF
setlocal
set drv=%1
set drv=%drv:"=%
set nn=%2
set nn=%nn:"=%
set mn="%TEMP%\MapName_%RANDOM%.VBS"
set er="%TEMP%\MapName_%RANDOM%.TMP"
if exist %er% del /q %er%
@echo Set oShell = CreateObject("Shell.Application")>%mn%
@echo DRIVE = "%drv:~0,1%:\">>%mn%
@echo oShell.NameSpace(DRIVE).Self.Name = "%nn%">>%mn%
set OK=N
call :quiet>%er% 2>&1
del /q %mn%
call :sz %er%
del /q %er%
endlocal&set %3=%OK%
goto :EOF
:sz
set /a size=%~z1
if %size% EQU 0 set OK=Y
goto :EOF
:quiet
cscript //nologo %mn%



Hide comments

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.
Publish