5

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?

Joey
  • 344,408
  • 85
  • 689
  • 683
ArmlessJohn
  • 53
  • 1
  • 3

3 Answers3

13

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.

PA.
  • 28,486
  • 9
  • 71
  • 95
jeffd
  • 131
  • 2
3

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
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

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.

Logan Capaldo
  • 39,555
  • 5
  • 63
  • 78
  • 1
    +1 In batch script there's no such thing as lousy; if it does the job then it's good stuff!! ;-) – David Heffernan Jan 30 '11 at 19:55
  • Thank you, this is what I am looking for, but unfortunately it seems to be returning errorlevel 1 for everything. I do a " `dir /A:L %Link%>nul 2>nul` " followed by a " `if errorlevel 1 GOTO :LinkJunctionExists "%Link%"` ", is this right? – ArmlessJohn Jan 30 '11 at 20:03
  • Thanks for your edited answer. Having admin rights is not an issue, and being a reparse point is enough for me. Unfortunately I have tested this - and it does indeed return an error if it is not a junction - but it does not set the errorlevel as you described (it always stays at 0). – ArmlessJohn Jan 30 '11 at 20:42
  • NEVERMIND, there was something wrong with my terminal. Thanks a lot, this worked! – ArmlessJohn Jan 30 '11 at 20:44
  • 2
    @David: Not quite, as requiring administrative privileges is a fairly hefty requirement, actually. – Joey Jan 30 '11 at 23:31