(view source code of truename.bat as plain text)
@ECHO OFF
:: Check Windows version and command line arguments
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
IF "%~1"=="" GOTO Syntax
IF NOT "%~2"=="" GOTO Syntax
IF NOT EXIST "%~dp1" GOTO Syntax
:: Do not test UNC paths
ECHO "%~f1" | FINDSTR.EXE /R /B /I /C:"""\\\\[A-Z]"
IF NOT ERRORLEVEL 1 GOTO:EOF
SETLOCAL ENABLEDELAYEDEXPANSION
SET MyPath="%~f1"
SET MyDrive="%~d1"
:AllOverAgain
SET TrueName=%MyPath%
:: Find out if the drive is a mapped network drive,
:: and if so translate the drive letter to its UNC path
FOR /F "tokens=1* delims=\" %%A IN ('NET.EXE USE %~d1 2^>NUL ^| FIND.EXE "\\"') DO (
REM Nested FOR loop removes leading spaces
FOR /F "tokens=* delims= " %%C IN ("%%~B") DO (SET TrueName="\\%%~C%~pnx1")
REM UNC path will not be tested any further
ECHO !TrueName!
ENDLOCAL
GOTO:EOF
)
:: Check if the drive is SUBSTituted, and if so find out its true path
:: Delims is a greater than, followed by a tab and a space
FOR /F "tokens=2* delims=> " %%A IN ('SUBST.EXE ^| FINDSTR.EXE /B "%~d1\: =>"') DO (
SET Subst=%%B
IF "!Subst:~0,4!"=="UNC\" SET Subst=\!Subst:~3!
SET TrueName="!Subst!%~pnx1"
SET MyPath=%TrueName%
GOTO AllOverAgain
)
:: Check for file/directory junctions, starting at the specified folder, then its parent, etc.
SET TestDir=%TrueName%
:Iterate
FOR /F "tokens=*" %%A IN (%TestDir%) DO (SET TestSub=" %%~nxA [")
FOR /F "tokens=*" %%A IN ("%TestDir:~1,-1%.\..") DO (SET TestDir="%%~fA")
FOR /F "tokens=2 delims=[]" %%A IN ('DIR /ADL !TestDir! 2^>NUL ^| FIND.EXE /I %TestSub%') DO (
SET TrueName=!TrueName:%TestDir:~1,-1%\=%%~dpA!
SET MyPath=!TrueName!
GOTO AllOverAgain
)
FOR %%A IN (%TestDir%) DO (
IF NOT "%%~pA"=="\" (
GOTO Iterate
)
)
ECHO.%TrueName%
:End
ENDLOCAL
GOTO:EOF
:Syntax
ECHO.
ECHO TrueName, Version 3.00 for Windows NT 4 and later
ECHO Displays the true path of a file or directory, like COMMAND.COM's internal
ECHO TRUENAME command; handles SUBSTituted drives as well as mapped network
ECHO drives and directory junctions, and even some combinations.
ECHO.
ECHO Usage: TRUENAME "d:"
ECHO or: TRUENAME "d:\path"
ECHO or: TRUENAME "d:\path\filename.ext"
ECHO.
ECHO As always, enclosing paths and file names in doublequotes is recommended.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
page last modified: 2024-04-16; loaded in 0.0056 seconds