4.7. Creating a Link or Junction Point


You want to create a link to a folder. This is sometimes referred to as a junction point. Links can be created only on NTFS file systems. Junction points are useful if you want to create a simplified path to a folder that is nested deeply in the file system.


Using a command-line interface

The linkd.exe command from the Resource Kit can create a link:

> linkd <LinkName> 

This creates a link from folder c:\program files\perl to c:\perl:

> linkd c:\perl "c:\program files\perl"

This removes the link to perl.exe:

linkd c:\perl /d

You can also use the Sysinternals junction.exe tool to create and delete links:

> junction c:\perl "c:\program files\perl"
> junction /d c:\perl

A cool thing about junction.exe is that you can also use it to search for links:

> junction /s c:\

If you are browsing the file system with Windows Explorer, you won't be able to differentiate links from normal files and folders, but in a CMD session you can. A link shows up as <JUNCTION>, as shown here:

> dir 
 Volume in drive C is System
 Volume Serial Number is F0CE-2C6F
 Directory of C:\
01/02/2002  09:08 AM                 0 build.ini
10/06/2003  01:57 PM    <DIR>          Documents and Settings
11/02/2003  12:01 AM    <DIR>          Inetpub
11/18/2003  11:43 PM    <JUNCTION>     Perl
10/06/2003  02:14 PM    <DIR>          Program Files
11/16/2003  11:25 PM    <DIR>          scripts
12/04/2003  12:45 AM    <DIR>          WINDOWS
               6 File(s)    439,283,427 bytes
               7 Dir(s)   1,575,822,336 bytes free

Using VBScript

' This code creates a link by shelling out to the linkd command.
strLink   = "c:\perl"
strTarget = "c:\program files\perl"
' ------ END CONFIGURATION ---------
strCommand = "linkd " & strLink & " " & strTarget
set objWshShell = WScript.CreateObject("WScript.Shell")
intRC = objWshShell.Run(strCommand, 0, TRUE)
if intRC <> 0 then
   WScript.Echo "Error returned from running the command: " & intRC
   WScript.Echo "Command executed successfully"
end if


Links, or junction points, are different from shortcuts in that they are transparent to any process or application that accesses them. A shortcut is simply a file that redirects applications to a different location. A junction point is similar to a symbolic link in Unix. When you open a junction point, applications, such as Windows Explorer, behave as if you opened the source folder. The only difference is if you delete the junction point in Windows Explorer, the source directory isn't deleted—only the junction point is deleted.

See Also

MS KB 205524 (How to create and manipulate NTFS junction points)

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.