How can I make a batch file discover if a directory is actually a Junction?
Asked Answered
E

3

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?

Enugu answered 30/1, 2011 at 19:43 Comment(2)
Deleted my answer because I realized it wouldn't work on directory junctions as dir descends down into the directory when given a directory as a parameterAlumina
Oh ok, I thought that might have been the issue. Thank you anyway.Enugu
A
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.

Alumina answered 30/1, 2011 at 19:53 Comment(5)
+1 In batch script there's no such thing as lousy; if it does the job then it's good stuff!! ;-)Architecture
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?Enugu
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).Enugu
NEVERMIND, there was something wrong with my terminal. Thanks a lot, this worked!Enugu
@David: Not quite, as requiring administrative privileges is a fairly hefty requirement, actually.Bale
C
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.

Coordinate answered 1/3, 2011 at 10:47 Comment(0)
S
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
Sharasharai answered 5/6, 2014 at 14:53 Comment(0)
A
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.

Alumina answered 30/1, 2011 at 19:53 Comment(5)
+1 In batch script there's no such thing as lousy; if it does the job then it's good stuff!! ;-)Architecture
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?Enugu
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).Enugu
NEVERMIND, there was something wrong with my terminal. Thanks a lot, this worked!Enugu
@David: Not quite, as requiring administrative privileges is a fairly hefty requirement, actually.Bale

© 2022 - 2024 — McMap. All rights reserved.