Question

I'm writing a Batch file (.bat) and I couldn't find a way to discover if a given directory I have the path to is a real directory or a Junction (created on Windows 7 by using mklink /j). Can anyone point me in the right direction?

Was it helpful?

Solution

This is a lousy technique but fsutil reparsepoint query path to file will fail (%ERRORLEVEL% will be 1) if the file is not a junction and succeed (%ERRORLEVEL% will be 0) if it is one. The other problem with this is fsutil wants you to be an administrator. Additionally, not all reparse points are directory junctions.

OTHER TIPS

In a batch script you can use the following:

 SET Z=&& FOR %%A IN (linkfilename) DO SET Z=%%~aA
 IF "%Z:~8,1%" == "l" GOTO :IT_A_LINK

this is quicker than calling DIR /AL.

The %%~aA gets the attributes of the "linkfilename",
a 9 char string like d-------- (a directory),
or d-------l a link to a directory,
or --------l a link to a file.

%Z:~8,1% then grabs just the reparse point attribute.

I have this little gem which will list all Junctions and their targets in your current directory:

for /F "delims=;" %j in ('dir /al /b') do @for /F "delims=[] tokens=2" %t in ('dir /a ^| findstr /C:"%j"') do @echo %j :: %t

Example output:

Application Data :: C:\Users\AB029076\AppData\Roaming
Cookies :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Cookies
Local Settings :: C:\Users\AB029076\AppData\Local
My Documents :: C:\Users\AB029076\Documents
NetHood :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Network Shortcuts
PrintHood :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
Recent :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Recent
SendTo :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\SendTo
Start Menu :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Start Menu
Templates :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Templates
TestLink :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Network Shortcuts
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top