Sign in to follow this  
Followers 0
wraithdu

Determine if a Directory is a Junction

7 posts in this topic

This is almost too simple to post, but someone mentioned it in the beta thread. On Vista and 7 there are multiple junction points that point recursively within a user's profile. This is baaaad juju when recursively parsing a user profile with FileFindFirst/NextFile. Here's the function call to determine if a directory is a junction point. I left out a few sanity checks (that the directory exists and that it IS a directory) since YOU should be doing that before calling this function ;) Since this function would have to be called on every folder you find, making it short and simple is key.

Func _IsJunction($sDirectory)
    ; junctions have these attributes:
    ; FILE_ATTRIBUTE_HIDDEN = 0x2
    ; FILE_ATTRIBUTE_SYSTEM = 0x4
    ; FILE_ATTRIBUTE_REPARSE_POINT = 0x400
    Local Const $INVALID_FILE_ATTRIBUTES = -1
    Local Const $FILE_ATTRIBUTE_JUNCTION = 0x406
    Local $attrib = DllCall("kernel32.dll", "dword", "GetFileAttributesW", "wstr", $sDirectory)
    If @error Or $attrib[0] = $INVALID_FILE_ATTRIBUTES Then Return SetError(1, 0, -1)
    Return (BitAND($attrib[0], $FILE_ATTRIBUTE_JUNCTION) = $FILE_ATTRIBUTE_JUNCTION)
EndFunc


;; ===== EXAMPLE =====
$sPath = "C:\Users\" & @UserName
$search = FileFindFirstFile($sPath & "\*.*")
If $search <> -1 Then
    While 1
        $item = FileFindNextFile($search)
        If @error Then ExitLoop
        If @extended Then ; directory
            $fJunction = _IsJunction($sPath & "\" & $item)
            If @error Then $fJunction = "!ERROR!"
            ConsoleWrite("Dir:  " & $item & @TAB & @TAB & "Junction:  " & $fJunction & @CRLF)
        EndIf
    WEnd
    FileClose($search)
EndIf

Share this post


Link to post
Share on other sites



I did some research, and while this function is good for the default junctions in Windows, there can be user generated Junctions that are not Hidden or System. Also, files can have the Reparse Point attribute (though I don't think it does anything), so making sure the folder in question is actually a folder would be a good thing, especially in your loop.

Share this post


Link to post
Share on other sites

Hence:

I left out a few sanity checks (that the directory exists and that it IS a directory) since YOU should be doing that before calling this function

Regarding user-made junctions...I suppose that is possible. However I don't know if simply checking for FILE_ATTRIBUTE_REPARSE_POINT is enough...

Share this post


Link to post
Share on other sites

Been working on a utility to backup and restore junctions after it was pointed-out on another forum that once you lose the junctions for any reason there is no standard fix.

Alpha code at the moment (and none too tidy either) so suggest not using on a production computer yet.

Owing to the need to get the junction targets I used the cowboy approach of DIR >file, as I'm so far unaware of a DLL-call which will give the full info on a junction.

Also, used cmd.exe's MKLink to create junctions since Autoit's own FileCreateNTFSLink() creates unicode paths, and that doesn't seem to be what the originals used. Might not matter but I wanted the restore to be exact if possible.

Share this post


Link to post
Share on other sites

I know this is an old thread, but I found it using google.

The function at the top does not work for junctions that I make with the MKLINK command.

I'll try removing the attribute to 0x410 which just checks for directory and reparse point.

Seems to work.

Share this post


Link to post
Share on other sites

This is probably true. If I remember correctly MSDN had the hidden/system stipulation. But it is entirely possible it is not always true. Simply checking for the reparse attribute should be enough.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0