James Yeates Cells, Climbing, Computers

Finding UNC Paths with AI

Many people use mapped drives to access files stored on organisational shared drives. This leads to procedures being written with path references that only work for the author as others either don’t have the location mapped, or mapped under a different letter.

To sort this out you can create a batch file with the following:

@echo off
setlocal
 
:: This script prints the full UNC path for the current directory.
 
:: First, check if the current path already starts with \\
if "%CD:~0,2%"=="\\" (
    echo %CD%
    goto :End
)
 
:: If not, try to find a mapped network drive corresponding to the current drive letter.
set "UNC_PATH="
for /f "tokens=2,*" %%a in ('net use %CD:~0,2% 2^>nul ^| find "Remote name"') do (
    set "UNC_PATH=%%b%CD:~2%"
)
 
:: Display the UNC path if found; otherwise, display the original local path.
if defined UNC_PATH (
    echo %UNC_PATH%
) else (
    echo %CD%
)
 
:End
:: Wait for the user to press a key before closing the window
pause
 
endlocal
goto :EOF

Move this to the location you want the reference of and run it - job done.