Determine the mapped network path from cmd window

55

Solution 1

Type

net use

Which will shows you all currently connected network drive.

OK           Z:        \\127.0.0.1\c$            Microsoft Windows Network

Solution 2

The path of the bat may be different from the working directory. So we need Mykorrhiza's first approach inside a bat. To accommodate the situation of missing status and also local disk drives, we need additional checks. The following is the working code:

SET cNetworkPath=    
FOR /F "tokens=2" %%i IN ('NET USE ^| FIND "%CD:~0,2%"') DO (
      SET cNetworkPath=%%i)
if "%cNetworkPath%" == "%CD:~0,2%" (
  FOR /F "tokens=3" %%i IN ('NET USE ^| FIND "%CD:~0,2%"') DO (
        SET cNetworkPath=%%i)
)
if "%cNetworkPath%" == "" set cNetworkPath=%CD:~0,2%
SET cNetworkPath=%cNetworkPath%%CD:~2%
ECHO %cNetworkPath%

The above code works in most cases, but there are cases where the net use and the find do not work, the following is the finally tested work method:

SET cNetworkPath=
for /f "tokens=2" %%i in ('wmic path win32_mappedlogicaldisk get deviceid^, providername ^| findstr "%CD:~0,2%"') do (set cNetworkPath=%%i)
echo %cNetworkPath%

Solution 3

It's quite an old question but.. I was looking for the exact same answer as I was trying to create a batch that will use the UNC path to the actual location of the patch and do some things there (so only copy&paste to another location/folder and start again).

As I couldn't find an answer I found a solution myself, but it's not very beautiful and certainly not a simple command. But it's possible to implement in batch. On CMD it would be:

FOR /F "tokens=2" %i IN ('NET USE ^| FIND "%CD:~0,2%"') DO (
      SET cNetworkPath=%i)
SET cNetworkPath=%cNetworkPath%%CD:~2%
ECHO %cNetworkPath%

You can copy the four lines (better 4+empty line) and paste them into CMD to get an imidiate echo of the path to copy it.

In batch you would use it a bit differently:

FOR /F "tokens=2" %%i IN ('NET USE ^| FIND "%~d0"') DO (
      bNetworkPath=%%i)
SET bCheckPath=!bOriginalPath!%~p0

The variable %CD% stores the current path and you need only the drive letter so you only search for that with the FIND command in NET USE. With the "tokens=2" (or 3, depending on NET USE output) the %i variable stores the path to the drive letter you searched for. After that the second SET command adds the folders you browsed on the network drive with %CD:~2% (offset 2 to cut off the drive letter).

For batch you use the %~d0 or %~p0 variables. %0 stores the full path of the batch itself (e. g. Z:\temp\test.bat ; %~d0 = Z: ; %~p0 = \temp\ ; d = drive, p = path, f = full path, n = name) otherwise it's similar to the CMD command.

Solution 4

If you want it to always display it at your prompt, you could

set prompt=$M$Q$S$P

which will show you your UNC path and your drive letter based path.

Share:
55

Related videos on Youtube

Yasin Fakhar
Author by

Yasin Fakhar

Updated on September 18, 2022

Comments

  • Yasin Fakhar
    Yasin Fakhar over 1 year

    enter image description here

    I tried to execute this piece of java code in NetBeans but as I debug it step by step , whenever I bring the cursor of the mouse inside it to check the value of 'a' variable , It will be incremented strangely and the output shows the wrong answer. What is the problem?

    • Just_Alex
      Just_Alex about 4 years
      Using netbeans 11.2 and openjdk 11 debug shows a value of 10. Which is correct. Update netbeans maybe. If that is not the case, update jdk.
    • Yasin Fakhar
      Yasin Fakhar about 4 years
      I mean whenever I debug this I will get different answer in output . the output is not constant . Once I debug it and I got 42 in output. How is it possible?
    • Just_Alex
      Just_Alex about 4 years
      I'm not sure how you can get 42 in the output print of the console. If you are referring to the debug pop-up it is not meant to be reliable, but in netbeans-version 11.2 I found the pop-up 'a= 10'.
  • Ofiris
    Ofiris almost 11 years
    Thanks, do you know a way to get the full path of the current working directory?
  • Endoro
    Endoro almost 11 years
    What about echo %cd% ?
  • Ofiris
    Ofiris almost 11 years
    @Endoro, echo %cd% outputs the current directory (Z:\ABC) and not \\netDrive\ABC
  • Darius
    Darius almost 11 years
    I don't think there is a simple command line you can do to get it. You may be able to write a batch / powershell script to do it, but I haven't tried to make one. Check the answer from Icarus on: superuser.com/questions/244579/… maybe you can use it to your need.
  • Admin
    Admin over 8 years
    What about drives which are not currently connected (e.g., over a VPN which is currently disconnected)?
  • user1696603
    user1696603 almost 8 years
    the sample is intriguing, but broken. For example the (DO...) in batch example is missing SET ..., and bOriginalPath is not defined anywhere.
  • Yasin Fakhar
    Yasin Fakhar about 4 years
    No , You did not get the picture . I mean whenever I debug this I will get different answer in output . the output is not constant . Once I debug it and I got 42 in output. How is it possible?
  • Dan Serb
    Dan Serb about 4 years
    Is that the only code you have? Do you have a warning on 'a', or why is it in yellow?
  • Yasin Fakhar
    Yasin Fakhar about 4 years
    Yes , that is the only code and no warning . The color is Yellow because I drag it to check the value
  • user1696603
    user1696603 about 3 years
    Thanks Frank. I adapted your 2nd example into batch file that accepts the path to get UNC for as parameter, see stackoverflow.com/questions/21482825/…