.NET

Windows: removing file and directory reparse points (symbolic links, directory links, junctions, hard links

The interwebs is full of posts telling about how to create file and directory junctions**.

But there is little information about removing them and even less being correct: some suggest to del a directory junction (which just deletes everything in it but the junction).

Finally there is little information about listing all junctions, so lets start with that:

Deleting a link depends on the kind of link, not the kind of source.

Since symlink and hardlinks are for files, and directory symlink and junctions are for directories, this is how:

  • Delete a file symlink or hardlink by using DEL.
  • Delete a directory symlink or junction using RMDIR.

SysInternals – I wrote about them before – has a great junction tool. It can be used to create, delete and (optionally recursively) list reparse points. All usages allow for file and directory junctions.

More about reparse points

This is about the **: actually they are reparse points; for files they are symlinks, for directories mostly junctions, but sometimes symlinks.

And actually the reason I wrote this blog post. As you also have hardlinks. Some combinations of files and directories with these kinds of links fail.

Lets first go to see what kind of links there are on a fresh Windows system.

This is the only directory symlink: C:\Users\All Users and junction will show it like this:

.\\?\C:\\Users\All Users: SYMBOLIC LINK
   Print Name     : C:\ProgramData
   Substitute Name: \??\C:\ProgramData

It is unlike this directory junction C:\Users\Default User which junction will show as this:

\\?\C:\\Users\Default User: JUNCTION
   Print Name     : C:\Users\Default
   Substitute Name: C:\Users\Default

Together with C:\Users\Default and C:\Users\desktop.ini they are hidden, so you need the /AH flag to show them using DIR (as a gist, since WordPress still screws up less than and greater than):

Directory of C:\Users
08/22/2013  04:45 PM    <SYMLINKD>     All Users [C:\ProgramData]
09/30/2013  06:27 AM    <DIR>          Default
08/22/2013  04:45 PM    <JUNCTION>     Default User [C:\Users\Default]
08/22/2013  05:34 PM               174 desktop.ini

Default 08/22/2013 04:45 PM Default User [C:\Users\Default] 08/22/2013 05:34 PM 174 desktop.iniWhen you look at the examples below, it is odd to see that C:\Users\All Users is a SYMLINK and not a SYMLINKD as it points to a directory.

And yes, there are not so and very subtle differences between SYMLINKD and JUNCTION.

Lets show some examples.

The examples are hopefully more complete than the complete guide.

Since symlinks are client side created and not verified until use, you can actually use mklink to create both file and directory symbolic links for a file. DIR shows them as SYMLINK or SYMLINKD.

A SYMLINK to a file actually works, but a SYMLINKD or JUNCTION to a file gives you an Access Denied error. Hardlinks get the attributes of the source (so delete hidden hardlinks using the DEL /AH option).

Example batch file:

 

pushd %public%
cd ..
mklink desktop.ini.link desktop.ini
dir desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del desktop.ini.link
mklink /D desktop.ini.link desktop.ini
dir desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del desktop.ini.link
rd desktop.ini.link
mklink /J desktop.ini.link desktop.ini
dir desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del desktop.ini.link
rd desktop.ini.link
mklink /H desktop.ini.link desktop.ini
dir /ah desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del /ah desktop.ini.link
rd desktop.ini.link
popd

Example output:

C:\Windows\Temp>pushd %public%
C:\Users\Public>cd ..
C:\Users>
C:\Users>mklink desktop.ini.link desktop.ini
symbolic link created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir desktop.ini.link | findstr desktop.ini.link
04/05/2015  07:04 PM          desktop.ini.link [desktop.ini]
C:\Users>type desktop.ini.link
[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21813
C:\Users>del desktop.ini.link
C:\Users>
C:\Users>mklink /D desktop.ini.link desktop.ini
symbolic link created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir desktop.ini.link | findstr desktop.ini.link
File Not Found
 Directory of C:\Users\desktop.ini.link
C:\Users>type desktop.ini.link
Access is denied.
C:\Users>del desktop.ini.link
The directory name is invalid.
C:\Users>rd desktop.ini.link
C:\Users>
C:\Users>mklink /J desktop.ini.link desktop.ini
Junction created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir desktop.ini.link | findstr desktop.ini.link
File Not Found
 Directory of C:\Users\desktop.ini.link
C:\Users>type desktop.ini.link
Access is denied.
C:\Users>del desktop.ini.link
The directory name is invalid.
C:\Users>rd desktop.ini.link
C:\Users>
C:\Users>mklink /H desktop.ini.link desktop.ini
Hardlink created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir /ah desktop.ini.link | findstr desktop.ini.link
08/22/2013  05:34 PM               174 desktop.ini.link
C:\Users>type desktop.ini.link
[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21813
C:\Users>del /ah desktop.ini.link
C:\Users>rd desktop.ini.link
The system cannot find the file specified.
C:\Users>
C:\Users>popd
C:\Windows\Temp>

When you try this for directories, you are in for a few small surprises.

A SYMLINK to a directory neither works as file nor as directory. A SYMLINKD or JUNCTION to a directory works. Hardlinks don’t work for directories with reason: limit the risk of cycles.

Example batch file:

pushd %public%
cd ..
mklink desktop.ini.link desktop.ini
dir desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del desktop.ini.link
mklink /D desktop.ini.link desktop.ini
dir desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del desktop.ini.link
rd desktop.ini.link
mklink /J desktop.ini.link desktop.ini
dir desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del desktop.ini.link
rd desktop.ini.link
mklink /H desktop.ini.link desktop.ini
dir /ah desktop.ini.link | findstr desktop.ini.link
type desktop.ini.link
del /ah desktop.ini.link
rd desktop.ini.link
popd

Example output:

C:\Windows\Temp>pushd %public%
C:\Users\Public>cd ..
C:\Users>
C:\Users>mklink desktop.ini.link desktop.ini
symbolic link created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir desktop.ini.link | findstr desktop.ini.link
04/05/2015  07:04 PM          desktop.ini.link [desktop.ini]
C:\Users>type desktop.ini.link
[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21813
C:\Users>del desktop.ini.link
C:\Users>
C:\Users>mklink /D desktop.ini.link desktop.ini
symbolic link created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir desktop.ini.link | findstr desktop.ini.link
File Not Found
 Directory of C:\Users\desktop.ini.link
C:\Users>type desktop.ini.link
Access is denied.
C:\Users>del desktop.ini.link
The directory name is invalid.
C:\Users>rd desktop.ini.link
C:\Users>
C:\Users>mklink /J desktop.ini.link desktop.ini
Junction created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir desktop.ini.link | findstr desktop.ini.link
File Not Found
 Directory of C:\Users\desktop.ini.link
C:\Users>type desktop.ini.link
Access is denied.
C:\Users>del desktop.ini.link
The directory name is invalid.
C:\Users>rd desktop.ini.link
C:\Users>
C:\Users>mklink /H desktop.ini.link desktop.ini
Hardlink created for desktop.ini.link <<===>> desktop.ini
C:\Users>dir /ah desktop.ini.link | findstr desktop.ini.link
08/22/2013  05:34 PM               174 desktop.ini.link
C:\Users>type desktop.ini.link
[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21813
C:\Users>del /ah desktop.ini.link
C:\Users>rd desktop.ini.link
The system cannot find the file specified.
C:\Users>
C:\Users>popd
C:\Windows\Temp>

Conclusion

  • symlink and hardlink can be used as files, but not as directories.
  • files referenced through symlinkd and junction behave as empty directories.
  • symlinkd and junction can be used as directories, but not as files.
  • directories referenced as symlink are not usable.
  • directories cannot function as hardlink source.
  • hardlinks to files inherited their attributes.

Related Articles

Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button