You have three methods
Solution 1: fsutil reprocessing point
Use symlink / connection to fsutil reparsepoint query and check %errorlevel% for success, for example:
set tmpfile=%TEMP%\%RANDOM%.tmp fsutil reparsepoint query "%DIR%" >"%tmpfile%" if %errorlevel% == 0 echo This is a symlink/junction if %errorlevel% == 1 echo This is a directory
This works because the fsutil reparsepoint query cannot do anything in the standard directory and throws an error. But a permission error also causes %errorlevel%=1 !
Solution 2: dir + find
List the links of the parent directory with dir , output the filter with find and check %errorlevel% for success, for example:
set tmpfile=%TEMP%\%RANDOM%.tmp dir /AL /B "%PARENT_DIR%" | find "%NAME%" >"%tmpfile%" if %errorlevel% == 0 echo This is a symlink/junction if %errorlevel% == 1 echo This is a directory
Solution 3: for (best)
Get the attributes of a directory with for and check the last of it because it points to links. I think this is the smarter and best solution.
for %i in ("%DIR%") do set attribs=%~ai if "%attribs:~-1%" == "l" echo This is a symlink/junction
FYI: this solution is not dependent on %errorlevel% , so you can also check for the correct errors!
Sources
source share