Get UNC of Mapped Drive

Microsoft Windows xp professional w/serv...
December 21, 2009 at 14:53:01
Specs: Windows XP
I am creating a batch file which will map a new network drive. I know how to check if the drive letter already exists and I know how to delete the previous assignment with net use, but I'm wondering if there is a way to find out the UNC of the previously mapped drive so I can automatically map it to a different drive letter. Thanks!

See More: Get UNC of Mapped Drive

Report •


#1
December 22, 2009 at 08:45:43
"The UNC of the previously mapped drive" - if you mean that
literally, then no, you can't get it after you've deleted it.

If you still have the drive mapped (before you delete it):

for /f "tokens=2*" %%a in ('net use z:^|find "Remote name"') do
set unc=%%b


Report •

#2
December 22, 2009 at 11:51:15
Just list them out, BEFORE you do the unmap:

C:\>net use
New connections will be remembered.

There are no entries in the list.


C:\>


(I have no mappings)


Report •

#3
December 29, 2009 at 17:00:52
Klint, sorry I don't understand the code you suggest. Yes, the drive would still be mapped. I'd want to find out the map location and reassign it before disconnecting.

My code would look something like this, with the bits I haven't figured out in brackets:

rem check if correct P: drive already mapped
IF EXIST P:\Templates GOTO ALREADY

rem check if P:drive mapped to something else
IF NOT EXIST P:\NUL GOTO MAP

rem Find out path of user's current P: drive
[missing code]

rem Map location of user's current P: drive to next available
[missing code]

rem Disconnect old P: drive
net use P: /d /yes

:MAP
net use P: %SHARE_PATH% /persistent:yes
echo Share drive successfully mapped.
pause
GOTO END

:ALREADY
echo Share drive was already mapped.
pause

:END


Report •

Related Solutions

#4
December 30, 2009 at 16:09:49
You should insert my code (edited to suit your needs) into the parts labelled [missing code] in your code. I have done this below, and also restructured your code a little:
setlocal enabledelayedexpansion
rem check if correct P: drive already mapped
IF EXIST P:\Templates (
    echo Share drive was already mapped.
    pause
) else (
    rem check if P:drive mapped to something else
    IF EXIST P:\NUL (
        rem Find out path of user's current P: drive
        for /f "tokens=2*" %%a in ('net use P:^|find "Remote name"') do (
            set PREVIOUS_PATH=%%b
        )

        rem Map location of user's current P: drive to next available
        net use * !PREVIOUS_PATH!

        rem Disconnect old P: drive
        net use P: /d /yes
    ) else (
        net use P: %SHARE_PATH% /persistent:yes
        echo Share drive successfully mapped.
        pause
    )
)


Report •

#5
December 31, 2009 at 09:14:28
That worked great! Thanks!!!

Report •


Ask Question