Cannot create a file when that file already exists
Cannot create a file when that file already exists
I’m trying to create a symbolic link between two directories. I have a directory called TestDocs and TestDocs2. I will be doing all my work in TestDocs, but I need it all to be reflected in TestDocs2. So all files that are in TestDocs2 will be replicated in TestDocs, and if I add a file, change a file, etc in TestDocs it should be reflected in TestDocs2.
So I thought it would be as simple as just doing this:
But when I do that I get the error:
Cannot create a file when that file already exists
Why am I getting this?
Also, do I have the order of my TestDocs and TestDocs2 wrong in the command?
Thanks for the help, Symbolic Links have always confused me!
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The correct usage is:
You’re creating a link, so the link is the new link you’re about to create.
And the target is the link’s target, which is the existing directory.
Here is how that worked for me. I wanted to relocate my C:\ProgramData\Package Cache to F: partition.
Steps I had to do:
In cmd run (all in one line, split here for readability)
I did this for Google Picasa so I could access photographs on more than one machine. It works very well as Picasa is not really a netowrk enabled program. For me I did the following:
Run the following commands from an elevated cmd prompt:
mklink /D «c:\Users\\AppData\Local\Google\Picasa2» «o:\PicasaDatabase\Picasa2»
mklink /D «c:\Users\\AppData\Local\Google\Picasa2Albums» «o:\PicasaDatabase\Picasa2Albums»
Two symbolic links will have been created in the source location and they will point to the network drive.
Obviously using symbolic links like this is a workaround for application that are not networkable and only one user can access the database at once.
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:
Although I am a beginning programmer and know some things about the CLI, I can’t figure out my mistake.
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.
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 :).
mklink error: Cannot create a file when that file already exists
I am trying to create a symbolic link on my win7 64bit machine to redirect the iTunes backup data to another drive. I am pretty sure the syntax is correct but cannot understand how to clear this error. I have tried both commands below which have the same error. I am running in Administrator Command Window. Could there be some corporate group policy preventing me from running this command?
Cannot create a file when that file already exists.
5 Answers 5
the syntax is incorrect. mklink has the following synatx:
Target is the file/folder that exists, and Link is the created one that links to the target.
so the command should be:
See the Microsoft Documentation for mklink usage.
The simplest way is to delete the Backup folder in the original Apple Computer folder on the C:\ drive, but leave the MobileSync folder be. This is because you’re trying to fake the existence of the Backup folder (so it must not exist already), but you also need its parents to exist.
Then, if we run the command:
mklink /J «%AppData%\Apple Computer\MobileSync\Backup» «E:\iTunes Backups»
Windows will hence create a hard link shortcut Backup on the C:\ drive.
Note: Make sure you use straight quotes, otherwise you get a syntax command error.
I just wanted to add that the error you mentioned in your comment above System cannot find the path specified appears to happen when you are symbolically linking at a folder sub-level which doesn’t exist at the link’s original source.
Meaning you can’t link
It will not work unless at least
exists as part of the link source. You would have to either link
or you would have to create at least an empty folder at
and then create the link
Ransomware Protection can also cause this when you have «Controlled Folder Access» enabled.
Not the answer you’re looking for? Browse other questions tagged windows or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Cannot create a file when that file already exists when using Directory.Move
I am trying to move the directory from one location to another location on the same drive. I am getting «Cannot create a file when that file already exists» error. Below is my code.
could any one suggest on this?
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
As both of the previous answers pointed out, the destination Directory cannot exist. In your code you are creating the Directory if it doesn’t exist and then trying to move your directory, the Move Method will create the directory for you. If the Directory already exists you will need to Delete it or Move it.
Something like this:
«This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. You must specify «c:\public\mydir» as the destDirName parameter, provided that «mydir» does not exist under «c:\public», or specify a new directory name such as «c:\newdir».»
You don’t need to create Directory first, it will throw IO Exception, if destination directory exists, Move method automatically creates it for you:
More infomation of Directory.Move :
This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists.
But, in your method, you are creating the destination directory before you move.
So, you need to change your method from
You can just call
What it does internally is it creates the target directory if it’s not exists and then it iterates over the source directory’s files and moves them to the target directory. That way the problem of «Cannot create a file when that file already exists» won’t happen.
You’ll need to add Microsoft.VisualBasic as a reference.
Not the answer you’re looking for? Browse other questions tagged c# or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Cannot create a file when it already exists using File.Move
I was trying to move a file from my Resx to my PC, but I’m keep having problems.
So I import a folder named «bad» in the Resources and I use the File.Move method to move the folder «bad» into my PC. But the program keeps crashing because it says: Cannot create a file when its already exists.
Here the code I use:
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The destination Directory cannot exist. In your code you are creating the Directory if it doesn’t exist and then trying to move your directory, the Move Method will create the directory for you. If the Directory already exists you will need to Delete it or Move it.
Destination supposed to have the filename as well
The MSDN documentation will only move files from a source to a destination, while Directory.Move will move the directory itself.
If I misunderstood you, and you want to move a file;
You can check if the file exists before or not using something like:
Edit: If you want to iterate through the directory and make sure that the file doesn’t exist before moving it, you can use something like:
Источники информации:
- http://stackoverflow.com/questions/63657111/unable-to-create-symlink-cannot-create-a-file-when-that-file-already-exists
- http://superuser.com/questions/597551/mklink-error-cannot-create-a-file-when-that-file-already-exists
- http://stackoverflow.com/questions/12667770/cannot-create-a-file-when-that-file-already-exists-when-using-directory-move
- http://stackoverflow.com/questions/22228542/cannot-create-a-file-when-it-already-exists-using-file-move