windowscmdcommand-line-interfacesymlinkmklink

Unable to create Symlink - cannot create a file when that file already exists


TLDR; When running the command below I get the "cannot create a file when that file already exists" error. I am very confused regarding everything I read on symlinks, since it is basically always unclear which directory is which and whether both or one of the directories at hand need to exist already. So I posted my command below and I hope that somebody can spot the mistake (and explain it in simple terms) :).


Recently, my C-drive (SSD) got full and I decided to move some things over to my D-drive (regular harddrive). I read about symlinks and tried to link a directory from C:\ to D:, but I always get the "cannot create a file when that file already exists" error. My command is the following:

C:\users\me\documents\main-folder\folder-I-want-linked > mklink /J "D:\folder\another folder\folder-where-I-want-to-store-the-docs" "C:\users\me\documents\main-folder\folder-I-want-linked"

Although I am a beginning programmer and know some things about the CLI, I can't figure out my mistake.

  1. I thought that maybe I should reverse the two directories. The sources I read are confusing to me: for example, the official Microsoft documentation talks about and , which could refer to both directories depending on how you look at it. I am a little confused as to how symlinks are supposed to point: in my common sense, but I would think this syntax is correct according to this page from howtogeek.com .

  2. I thought that, since the error is that something already exists, my empty map should be removed since, maybe, it would be created? In this regard I find this accepted answer at superuser.com very confusing. I also saw this accepted answer here at stackoverflow.com, saying that the destination directory cannot exist? Because this is the location where I want to put my files, I find this very weird. And from another source (which I cannot find this quick) I understood that both directories already need to exist.

  3. I thought that the map in which I ran the command would be incorrect. Running the command one layer above this one (so inside "main-folder") however also gave me the error; so did trying this from the D-drive location. But then, from the aforementioned howtogeek.com source, I got the idea that I don't need to be inside a specific directory?

I also saw a lot of questions on this website regarding symlinks, but I have not found any answer that could help me. I am probably making things waaaaay to complicated here, so if anyone has a simple correction/explanation, I would greatly appreciate that :).


Solution

  • Thanks to @Mofi, I figured out my mistakes:

    1. The empty folder (folder-I-want-linked on my C-drive) should not yet exist: it will be made upon creating the link;
    2. This also means that the symlink should be made from the parent directory (C:\users\me\documents\main-folder);
    3. I should use /D instead of /J.
    4. For some reason spelling out the absolute path to the folder I wanted to create did not work (C:\...\...\folder-I-want-linked), however the relative path from my current location (so simply folder-I-want-linked) did.

    The correct syntax for my example:

    C:\users\me\documents\main-folder> mklink /D "folder-I-want-linked" "D:\folder\another folder\folder-where-I-want-to-store-the-docs"