Junction Folders

The Problem

Recently on a project, I encountered following problem: an application had three sets of the same files in different folders.

Not very handy to install the application and completely inefficient considering disk space and maintenance. From my Linux background I immediately thought of symbolic links (symlinks, a folder that is a pointer to another folder).

I was under the impression that Windows did only know something like shortcuts. Shortcuts are very handy for users, double-click and you’ve in the correct folder, but useless for applications.

Junction Points come to the rescue…
During a weekend I started digging and discovered I could not be further away from the truth. Since Windows 2000 there is something like Junction Points, read Window’s implementation of symlinks (not 100% but it goes a long way). It is only supported on NTFS file-systems but that shouldn’t be a limitation anymore (you should be careful about your FAT-intake anyway ;)).

Since the introduction of Vista, Junction Points are fully integrated within the OS and you don’t need to install any toolkit anymore. Just from a cmd-prompt (you need admin-rights):

mklink [[/D] | [/H] | [/J]] link target
/D   Creates a directory symbolic link. Default is a file symbolic link.
/H   Creates a hard link instead of a symbolic link.
/J   Creates a Directory Junction.
link Specifies the new symbolic link name. 
target Specifies the path (relative or absolute) that the new link refers to.

For the pre-VISTA users there are a lot of tools and resource kits you can download from Microsoft. However there is one compact solution from Sys Internals in the form of junction.exe (see http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx)

usage: junction.exe [-s] [-q] <file or directory>
-q     Don't print error messages (quiet)
-s ;   Recurse subdirectories

usage: junction.exe [-d] <junction directory> [<junction target>]
-d     Delete the specified junction
example: junction d:\link c:\winnt

Something good to know for the pre-VISTA users: there are some side-effects in using files in junction folders linked to the recycle bin when you’ve using Windows Explorer to delete file in these folders. Depending on the size files are really deleted or just flagged for deletion. They disappear when you recycle the recycle bin. On http://en.wikipedia.org/wiki/NTFS_junction_point you’ll find more details about using commands like dir, rmdir in combination with junction folders. In VISTA all these problems are gone.

The solution

For the problem indicated above, I now have one folder like before and two junction folders pointing to this folder. Example:

Directory of E:\junction_example
14/12/2008  13:03    <DIR>          .
14/12/2008  13:03    <DIR>          .. 
14/12/2008  13:00    <DIR>          copy_1 14/12/2008  13:03    <JUNCTION>     copy_2 [\??\E:\junction_example\copy_1]
14/12/2008  13:03    <JUNCTION>     copy_3 [\??\E:\junction_example\copy_1] 

You even can assign specific access rights to junction folders independent of the target folder that the junction folder points to.

Also useful for…

A typical case in which junction folders are used in the Linux/Unix world, is for the installation of a new version of an application.

Example: MyApplication-2.0 is just copied to a sub-folder and the junction folder MyApplication points to the latest version Berfore the installation:

Directory of E:\applications
14/12/2008  13:11    <DIR>          . 
14/12/2008  13:11    <DIR>          .. 
14/12/2008  13:10    <JUNCTION>     MyApplication [\??\E:\applications\MyApplication-1.0] 
14/12/2008  13:11    <DIR>          MyApplication-1.0 

After the installation:

Directory of E:\applications
14/12/2008  13:12    <DIR>          . 
14/12/2008  13:12    <DIR>          .. 
14/12/2008  13:10    <JUNCTION>     MyApplication [\??\E:\applications\MyApplication-2.0] 
14/12/2008  13:11    <DIR>          MyApplication-1.0 14/12/2008  13:12    <DIR>          MyApplication-2.0 

Rolling back to the old version becomes very simple: just let the junction folder point back to the old version.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.