Code Scraps: Symbolic Links on Windows; Junctions and Hard Links

Did you know Windows can do its own equivalent of Linux Symbolic Links?

It’s been able to do them for quite a while, but it’s not something a lot of people seem familiar with. I only discovered the functionality as I had a directory of music I had ripped from my CD collection and I wanted to create a way to have all my ripped CD’s sorted into genre without changing the primary music directory.

Here’s an example scenario

I have a directory called Music which contains all my ripped CD’s, stored by artist / album. I use a DLNA server (in my case TVersity, but any DLNA server would be fine). And It’s all nice to have my music accessible from anywhere in the house – but all my music is in that single “Music” directory. What if I have a craving to listen to only listen to some Pop, Rock, Metal, or Jazz? How can I filter my music to just the genre I feel in the mood for?

This is where you can get a bit clever with Windows Junctioning.

Here’s a sample layout of the Music directory

M:\Music\Queen\Greatest Hits\
M:\Music\Queen\News of the World\
M:\Music\Daft Punk\Discovery\
M:\Music\Daft Punk\Random Access Memories\
M:\Music\David Bowie\Space Oddity\
M:\Music\David Bowie\Black Star\
M:\Music\Kiss\Hotter Than Hell\
M:\Music\Kiss\Love Gun\
M:\Music\Various\Now That’s What I Call Music\31\
M:\Music\Various\Now That’s What I Call Music\84\

Now I want to have an additional directory called “Music Genres”, where I store the same tracks like:

M:\Music Genres\Electronic\Daft Punk\Discovery\
M:\Music Genres\Electronic\Daft Punk\Random Access Memories\
M:\Music Genres\Jazz\Daft Punk\Random Access Memories\
M:\Music Genres\Pop\Daft Punk\Discovery\
M:\Music Genres\Pop\Daft Punk\Random Access Memories\
M:\Music Genres\Pop\Queen\Greatest Hits\
M:\Music Genres\Pop\Various\Now That’s What I Call Music\31\
M:\Music Genres\Pop\Various\Now That’s What I Call Music\84\
M:\Music Genres\Rock\Queen\Greatest Hits\
M:\Music Genres\Rock\Queen\News of the World\
M:\Music Genres\Rock\Kiss\Hotter Than Hell\
M:\Music Genres\Rock\Kiss\Love Gun\

So in the above “Music Genres” layout, you’ll see some albums have more than one genre – Queen, Bowie and some Kiss tracks all cover Pop and Rock. But storing physical duplicates of the songs is wasteful and and inefficient. If we utilise Windows Junctions we can create this structure without creating copies of the original files themselves.

Once the Junctions are created – I’ll have the Music Genres directory as I want, but the artist directories will be links to the files stored in M:\Music instead of being copies.

It’s important to note that these paths are not Windows Shortcuts

Shortcuts are “.lnk” files. They don’t provide a valid file-system path that computer programs can use. Shortcuts are designed for user convenience and are typically used to create program shortcuts in the Start Menu or Desktop. If you want a program (in this case a DLNA server) to be able to read the Genres directory, it needs to physically see files. So this is where Junctions come in as they create physical links on disk that a program can follow.

Create a Junction

So, first thing’s first – we need a few directories made:

C:\>m:
M:\>mkdir “Music Genres”
M:\>cd “Music Genres”
M:\Music Genres\>mkdir Pop
M:\Music Genres\>mkdir Rock

So now I have my Pop and Rock directories. Time to create a Junction.

Junction syntax

The Junction command is “mklink /J” followed by the target then the source (We’ll leave the debate about why Microsoft did target-source instead of source-target which seems more logical to another post).

M:\Music Genres\>cd Pop
M:\Music Genres\Pop\>mkdir Queen
M:\Music Genres\Pop\>cd Queen
M:\Music Genres\Pop\Queen\>mklink /J “Greatest Hits\” “M:\Music\Queen\Greatest Hits\”
M:\Music Genres\Pop\Queen\>cd ..\..
M:\Music Genres\>cd Rock
M:\Music Genres\Rock\>mkdir “Queen”
M:\Music Genres\Rock\Queen\>mklink /J “Greatest Hits\” “M:\Music\Queen\Greatest Hits\”

Having run the commands above, I know have a Pop and Rock directory that contain the Greatest Hits directory, and if I target my DLNA server against the “Music Genres” directory, I’ll see in the DLNA Server’s folders view that I now have those directories with the music available in them.

This all may seem a bit confusing, but give it a try.

Deleting Junctions

Great care should be taken when removing Junctions from your computer. The wrong commands and actions can cause loss of the original data! Windows doesn’t consistently treat Junctions the same depending on how you delete them. Some deletes will only remove the Junction, whilst other deletion methods will follow the Junction to the original files/directories and delete them.

The safest way to delete that I use is to open up Command Line and use the “rmdir” command on the Junction. The ‘rmdir’ command in a Command Line windows will ONLY delete the Junction. Do not use Powershell! If you use Powershell and deleting a Junction using rmdir, Powershell will also follow the junction and delete the underlying files/directories.

If you attempt to delete using any other means (eg right-clicking the junction, choosing delete). BACK UP YOUR DATA FIRST, then validate the deletion method works how you expected it to.

Found this useful?

Hopefully you will have found this guide useful. Please drop me a comment if you have any comments or recommendations.

Leave a Reply

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