HOME
Stephen Knight
09/15/2012 11:20 PM
Get UNC path from file path

Type:

Batch/Command file

Category:

Batch, UNC, Drive letter, Drive, Path
This simple batch script take a filename with a drive letter and uses the net use command to get the UNC path that drive is mapped to and returns the full UNC path. As it stands no error checking to make sure a drive letter has been passed, or that it IS a mapped drive etc. but shows the concept. It can be used from a command prompt by passing the filename required on the commandline, or a file can be dragged to the batch file from explorer and it will do the same.

As it stands the batch file sets the variable %uncname% to the name with UNC path.

Dec 2013 -- Markus Szumovski contacted me and advised of this not working on his Windows 7 as the output of the Net Use command is different - in my version the command "NET USE S:" for a network drive returns "Remote Name: \\server\share", in his (assumed to be Austrian) version it was showing "Remotename" without the space. He also added some suggestions for adding this to the "Right click" context menus in Explorer. Revised version in code and shown with his comments below.

I have used Markus additions and added my own way (Revised v2) which instead looks for a line containing the "\\" unc path and then strips off anything that is before it using

set uncname=%unc:*\\=\\%!pnx!

this takes the unc variable which now contains the whole line from the NET USE command, and replaces *\\ with \\, i.e. anything before the \\ on a line is removed.


@Markus:
I've used your "Get UNC path from file path" script for some while now on Windows 7, but I had to change it a bit to work for me (part of it could be because it's a Windows 7 machine).

First, I had to change the for-loop a bit so it correctly finds the remotename of the machine, this could be because of a different output format in Windows 7 net use, but I haven't checked it against a Windows XP or Vista machine... anyway, this way it works with Windows 7 64 bit net use now.

Secondly I built an "if" clause into it so it checks if it's really a network path and if it's not a network path it will retain the drive letter at the beginning of the path.

Thirdly I used "setlocal enabledelayedexpansion" and the delayed expansion syntax in some parts so a bracket in the path doesn't screw up the whole thing

And fourthly I put the resulting path into the clipboard with the "clip.exe" program available in Windows 7.

Then I put it in my Program Folder and added the following keys/values to the registry so I can choose from the context menu of a folder or file the entry "Get UNC Path" and the local- or unc-path of the file or directory get copied in my clipboard.

HKEY_CLASSES_ROOT\*\shell\unc
@="Get UNC Path"
HKEY_CLASSES_ROOT\*\shell\unc\command
@="\"C:\\Program Files\\Batch\\getunc.bat\" \"%1\""
HKEY_CLASSES_ROOT\Directory\shell\unc
@="Get UNC Path"
HKEY_CLASSES_ROOT\Directory\shell\unc\command
@="\"C:\\Program Files\\Batch\\getunc.bat\" \"%1\""

This is great if you need to send the correct path by email within a company and you might not know if they have set the network directory to the same letter as you. So you can just use "Get UNC Path" from the context menu and paste the correct path into your e-mail. At least that's one way how I use it ;)

Markus has tested the version I suggested using \\ now in multiple languages:

You're right, the difference isn't the OS but the language. The new version searching for "\\" instead of "Remote" works very fine! I've installed a few other language packages and tested the net use command with them. Turns out the more complex languages (chinese, ...) and some of the smaller ones (estonian) just use the english text, other languages use their own text but the new version should work for them all.
The position would be 19 in german too, but while checking other languages I found out that in other languages the position of the beginning of the unc path varies.
Here is the "Remote name" line in different languages:

english:
Remote name \\server\subfolder
german:
Remotename \\server\subfolder
hungarian
Távoli név \\server\subfolder
finnish:
Etänimi \\server\subfolder
frensh:
Nom distant \\server\subfolder
italian:
Nome remoto \\server\subfolder
polish:
Nazwa zdalna \\server\subfolder
turkish:
Uzak ad \\server\subfolder

But once again, the new version searching for "\\" should work fine in every language, it certainly works for me :) many thanks!


Hide details for CodeCode
@echo off
if "%~1"=="" (
echo Usage: getunc Z:\somedir\somefile.txt
echo Output: \\server\share\somedir\somefile.txt
exit /b
)

set unc=
for /f "tokens=2* delims= " %%U in ('net use %~d1 ^| find /i "Remote name"') do set unc=%%V
set uncname=%unc%%~pnx1%
echo %uncname%
pause

Revised version #1 - where output is "Remotename" in NET USE S:
===============================================================

@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo Usage: getunc Z:\somedir\somefile.txt
echo Output: \\server\share\somedir\somefile.txt
exit /b
)

set pnx=%~pnx1%

set unc=
for /f "tokens=2* delims= " %%U in ('net use %~d1 2^> nul ^| find /i "Remotename"') do set unc=%%U
if "%unc%" == "" (
set uncname=%~d1!pnx!
) else (
set uncname=%unc%!pnx!
)
echo %uncname% | clip

Revised version #2 - should work for any output before the path
===============================================================

@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo Usage: getunc Z:\somedir\somefile.txt
echo Output: \\server\share\somedir\somefile.txt
exit /b
)

set pnx=%~pnx1%

set unc=
for /f "tokens=* delims= " %%U in ('net use %~d1 2^> nul ^| find /i "\\"') do set unc=%%U
if "%unc%" == "" (
set uncname=%~d1!pnx!
) else (
set uncname=%unc:*\\=\\%!pnx!
)
echo %uncname%
echo %uncname% | clip


Hide details for ExplanationExplanation

if "%~1"=="" (
echo Usage: getunc Z:\somedir\somefile.txt
echo Output: \\server\share\somedir\somefile.txt
exit /b
)
set unc=
for /f "tokens=2* delims= " %%U in ('net use %~d1 ^| find /i "Remote name"') do set unc=%%V
set uncname=%unc%%~pnx1%
echo %uncname%
pause
Shows usage details if there are no parameters passed to the command line.
Blanks out the UNC variable
Reads the output of the NET USE command to get the UNC. %~d1 is the drive letter for the filename passed
set the UNCNAME variable to be the UNC path found above and the rest of the filename. %~pnx1 means to show the [p]ath, file[n]ame, and e[x]tension of the filename pased.


Show details for ExamplesExamples
Hide details for AttachmentsAttachments
getunc-original.cmdgetunc-original.cmdgetunc-revision1.cmdgetunc-revision1.cmdgetunc-revision2.cmdgetunc-revision2.cmd