GetUNC.bat D:\path or GetUNC D:\path\file.ext
It is designed to echo the UNC path if mapped drive else just echo the fully qualified path to the file or directory.
Retruns errorlevel 0 for mapped drive, 1 for regular drive and -1 if path or file passed does not exist.
It echo's UNC or drive path and places a '\' at the end of string if a directory else no '\' for a file name.
If a second arg is passed it will place the result in the variable name passed rather than echo it.
For example:
GetUNC . UNC
would assign the working directory else the drive path to the UNC variable:
echo %UNC% would echo: \\My computer\utilities\batch files\
GetUNC "S:\Test Dir\My file.txt"
echo's: \\My Computer\Mapped Drives\S_Drive\Test Dir\My file.txt
GetUNC C:\Windows
echo's: C:\Windows\
GetUNC /? displays USAGE and examples.
It appears to work in Win7 (32 and 64 bit) and XP 32 bit after I tweaked it for those OS.
If someone with other OS could test and report back or find bugs I haven't thought of I would be grateful.
Code: Select all
@echo off&setlocal EnableDelayedExpansion EnableExtensions&goto :start
:GetUNC.bat
echo(
echo(USAGE: GetUNC.bat [Drive:\path\file] [UNCenv]
echo(
echo(Echo's fully qualified UNC path of Drive, path or filename given
echo(Echo's UNC path of working directory if no arguments given
echo(Optionally places result in 'UNCenv' variable without echo
echo(The envireonment variable, 'UNCenv' must be the second argument
echo(If the drive in question is not a mapped drive, echo's Drive:\path\etc
echo(Returns UNC status of drive, path or file in errorlevel
echo(
echo(Examples:
echo(
echo( GetUNC [[with no arguments]]
echo( Echo's the UNC \\path of the current drive:\directory
echo(
echo( GetUNC S:\
echo( Echo's \\My Computer\path\etc\S_Drive\
echo( The UNC path or drive path will include a trailing '\' if a directory
echo(
echo( GetUNC S:\TEMP
echo( Echo's \\My Computer\path\etc\S_Drive\TEMP\ ^(with trailing \^)
echo(
echo( GetUNC "my file.txt"
echo( Echo's the full UNC \\path\my file.txt or d:\path\my file.txt
echo( Paths or files containing spaces must be quoted
echo(
echo( GetUNC . UNC
echo( Places the UNC path of the working directory into the 'UNC' variable
echo(
echo( GetUNC ..
echo( Echo's the UNC path to the parent ^(or root directory if no parent^)
echo(
echo( GetUNC %%~d0 MyBatch_UNC_Drive
echo( Assigns just the base UNC path of the current batch script
echo(
echo( GetUNC "R:\My path\My File.txt" UNC_Full_Path_to_my_File
echo( Assigns the full UNC \\path\file or fully qualified drive:\path\file
echo(
echo(If the Drive has no UNC path, ie. Fixed drive, the Drive letter version
echo(will be echo'd or assigned instead of an UNC path
echo(
echo(Returns: 0 if Drive is mapped to an UNC path and \\path\etc exists
echo( 1 if d:\path\etc exists
echo( -1 if file or path does not exist
endlocal&exit/b 1
:start
set/a err=1,attr=0
set unc=&set "pth="
if "%~1"=="/?" goto :GetUNC.bat
if "%~1"=="" (call :start . unc&set err=!errorlevel!&goto :end) else (
if exist "%~f1" (set unc=%~f1&set pth=%~f1&set attr=%~a1) else (exit/b -1)
)
if %attr:~0,1% equ d if %pth:~-1% neq \ set "pth=%pth%\"
for /f "skip=1 tokens=2* delims= " %%U in ('@net use %unc:~0,2% 2^>nul') do set unc=%%V&goto :next
:next
if %unc:~0,1% equ \ (set err=0&set unc=%unc%%pth:~2%) else (set unc=%pth%)
:end
endlocal&(
if not "%~2"=="" (set %~2=%unc%) else (echo.%unc%)
)&exit/b %err%