3

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 :).

SimoneVbt
  • 85
  • 1
  • 1
  • 10
  • 1
    Well, the current directory on executing the command `mklink` is `C:\users\me\documents\main-folder\folder-I-want-linked`. So the directory `folder-I-want-linked` exists already in directory `%UserProfile%\documents\main-folder`. For that reason it is impossible to create a junction in `%UserProfile%\documents\main-folder` which has the name `folder-I-want-linked`. You have to change the current directory to `%UserProfile%\documents\main-folder`, next move the entire directory `folder-I-want-linked` to `D:\folder\another folder`, and then you can create the junction in `main-folder`. – Mofi Aug 30 '20 at 11:57
  • Thank you for your comment. I already tried removing the original folder (and just tried again), but that didn't work unfortunately... – SimoneVbt Aug 30 '20 at 12:09
  • Have you opened the command prompt window __as administrator__? I recommend to read [MKLink on SS64](https://ss64.com/nt/mklink.html) which is the best website for all [Windows CMD commands](https://ss64.com/nt/), better than the Microsoft documentation for [Windows Commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands). – Mofi Aug 30 '20 at 12:16
  • In other words use in a command prompt window opened as administrator `cd /D "C:\users\me\documents\main-folder"`, make sure there is neither a file nor a folder with name `folder-I-want-linked` by running `dir /A "folder-I-want-linked"` which should output an error message because of no file/folder found with that name and run last `mklink /J "folder-I-want-linked" "D:\folder\another folder\folder-where-I-want-to-store-the-docs"` to create the junction. – Mofi Aug 30 '20 at 12:26
  • @Mofi I tried as an administrator, but unfortunately that doesn't work either - I also understood from several sources that I would get another error message then (but thanks for clarifying that I need to add the /D to the command, because I was indeed struggling with that ;)). Though I am now wondering: should I remove the ```folder-I-want-linked``` from the command itself too? – SimoneVbt Aug 30 '20 at 12:36
  • You create a __symbolic link__ to a folder with using option `/D` instead of a __junction__ with using option `/J`. Well, a symbolic link to a folder is nowadays better than a junction. Important is that on drive `C:` in folder in which you want to create the symbolic link there is no file, folder, junction or symbolic link with the name you want to give the symbolic link (or junction). And make sure `D:\folder\another folder\folder-where-I-want-to-store-the-docs` is really an existing folder and not a file, symbolic link or junction on drive `D:`. – Mofi Aug 30 '20 at 12:39
  • @Mofi I have removed the original folder from C:\, while the folder on D:\ still exists, but simply changing /J to /D still gives me the same error :/ – SimoneVbt Aug 30 '20 at 13:16
  • What was the command line on executing `mklink`? Was it `C:\users\me\documents\main-folder>mklink /D "folder-I-want-linked" "D:\folder\another folder\folder-where-I-want-to-store-the-docs"` whereby `C:\users\me\documents\main-folder>` is the prompt indicating that `C:\users\me\documents\main-folder` is the current directory and `mklink /D "folder-I-want-linked" "D:\folder\another folder\folder-where-I-want-to-store-the-docs"` is the command? – Mofi Aug 30 '20 at 17:09
  • @Mofi that worked, thanks! My mistake appeared to be writing "folder-I-want-linked" as "C:\blablabla\folder-I-want-linked". Although I thought both ways were possible, this actually worked. – SimoneVbt Sep 01 '20 at 09:08

2 Answers2

5

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"
SimoneVbt
  • 85
  • 1
  • 1
  • 10
0

simply move to the directory from where link is to be created then delete the file to be linked,then run mklink /D "file to be linked" "from where to link"