Creating Symbolic Links (Windows)

Switch View :
ScriptFree
Creating Symbolic Links

The function CreateSymbolicLink allows you to create symbolic links using either an absolute or relative path.

Symbolic links can either be absolute or relative links. Absolute links are links that specify each portion of the path name; relative links are determined relative to where relative–link specifiers are in a specified path. Relative links are specified using the following conventions:

  • Dot (. and ..) conventions—for example, "..\" resolves the path relative to the parent directory.
  • Names with no slashes (\)—for example, "tmp" resolves the path relative to the current directory.
  • Root relative—for example, "\windows\system32" resolves to the <current drive>:\windows\system32. directory
  • Current working directory-relative—for example, if the current working directory is c:\windows\system32, "c:file.txt" resolves to c:\windows\system32\file.txt.
    Note   If you specify a current working directory–relative link, it is created as an absolute link, due to the way the current working directory is processed based on the user and the thread.

A symbolic link can also contain both junction points and mounted folders as a part of the path name.

Symbolic links can point directly to a remote file or directory using the UNC path.

Relative symbolic links are restricted to a single volume.

Example of an Absolute Symbolic Link

In this example, the original path contains a component, 'x', which is an absolute symbolic link. When 'x' is encountered, the fragment of the original path up to and including 'x' is completely replaced by the path that is pointed to by 'x'. The remainder of the path after 'x' is appended to this new path. This now becomes the modified path.

X: C:\alpha\beta\absLink\gamma\file

Link: absLink maps to \\machineB\share

Modified Path: \\machineB\share\gamma\file

Example of a Relative Symbolic Links

In this example, the original path contains a component 'x', which is a relative symbolic link. When 'x' is encountered, 'x' is completely replaced by the new fragment pointed to by 'x'. The remainder of the path after 'x', is appended to the new path. Any dots (..) in this new path replace components that appear before the dots (..). Each set of dots replace the component preceding. If the number of dots (..) exceed the number of components, an error is returned. Otherwise, when all component replacement has finished, the final, modified path remains.

X: C:\alpha\beta\link\gamma\file

Link: link maps to ..\..\theta

Modified Path: C:\alpha\beta\..\..\theta\gamma\file

Final Path: C:\theta\gamma\file

Related topics

Hard Links and Junctions

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Community Content

gwlong
I found that relative path must use driver letter

e.g. 

succesful:

mklink qtbase\.git\hooks\sanitize-commit d:qtrepotools\git-hooks\sanitize-commit

result fail:

mklink qtbase\.git\hooks\sanitize-commit qtrepotools\git-hooks\sanitize-commit


Alfred E. Neumann
Symbolic links are reparse points, you can create them on Windows 2000 and later!

http://msdn.microsoft.com/en-us/library/aa365460.aspx tells that symbolic links are reparse points.

http://msdn.microsoft.com/en-us/library/aa365505.aspx tells how to create and manipulate reparse points.

http://msdn.microsoft.com/en-us/library/aa363940.aspx tells how to detect and distinguish reparse points.

http://msdn.microsoft.com/en-us/library/aa365507.aspx tells that all these operations dont depend on a "symlink" file system filter driver.

Fortunately, such a filter driver is but available from http://homepage1.nifty.com/emk/symlink.html


Harry Johnston
on Windows XP, how does one create a 'symbolic link' (to different volume)
[You don't.  See http://msdn.microsoft.com/en-us/library/aa365680%28v=VS.85%29.aspx (the parent of this article) under "Supported Operating Systems": "Symbolic links are available in NTFS starting with Windows Vista."

However, a junction point might do just as well, although neither is a supported scenario AFAIK. - Harry]



im want to move c:\documents and settings to d:\ds

yes, i realize there will be lots of steps involved here...  but what exact software tool would i use to create the actual symbolic link definition?  also, where, exactly in the file system does this symbolic link definition get stored?  if i redirect c:\documents and settings to d:\ds, is this redirection actually stored in c:\$MFT ?  i am just curious to know exactly where and how the symbolic link definition is stored.

ps - please do not suggest 'do an unattended install of xp, using [unattendedgui] directive telling xp to change the location of the user profile folder' - for it doesnt actually work.  yes, the unattended install will create a new folder d:\ds, but it does NOT DO AWAY WITH the original c:\documents and settings folder!  one may also not delete the c:\documents and settings folder.  this method quite simply does NOT work!

Michael Quinlan
mklink
mklink is used in Windows 7 to create symbolic (and other) links. The documentation appears to be here: http://technet.microsoft.com/en-us/library/cc753194(WS.10).aspx

Conrad Braam
Command Line tools to accomplish the same task:

Junction.exe from Microsoft Technet / SysInternals: http://www.microsoft.com/technet/sysinternals/FileAndDisk/Junction.mspx

Linkd.exe, Mountvol.exe, and Delrp.exe from Microsoft Support: http://support.microsoft.com/kb/205524

[Conrad] 

Fsutil, and Junction, Linkd utilities only allow hardlinks or junction points to be created - the link created through this API is not a hardlink, which is what these tools manage. On Windows 7 I use a tool called mklink.exe - it may be in the SDK, which can create junction, hardlinks and symlinks. The article makes it clear that symbolic links allow link to a file on another volume, which hardlinks do not.


Jason R. Coombs
Clarification of behavior when target is specified as current working directory-relative
The docs state "If you specify a current working directory–relative link, it is created as an absolute link, due to the way the current working directory is processed based on the user and the thread."

Through experimentation, I determined this means that a link to d:foo.txt is always treated as D:\foo.txt, even if the user's current working directory on d: is not d:\ when the link is created or when the link is referenced.