Visual-studio – Command copy exited with code 4 when building – Visual Studio restart solves it

visual studio

Every now and then when I build my solution here (with 7 projects in it) I get the dreaded 'Command copy exited with code 4' error, in Visual Studio 2010 Premium ed.

This is because of the post-build event not being able to go through.

Here's what solves the problem, temporarily

  • Sometimes: A restart of Visual Studio and I'm able to build the solution
  • Sometimes: Both a restart of Visual Studio and my file manager of choice (Q-Dir 4.37) solves it.

Here's what the post-build event looks like:

xcopy "$(SolutionDir)Solution Items\References\*.dll" "$(TargetDir)" /Y

When you get the command copy exited with code [insert value] error, it's normally because of the following:

  • read / write permissions
  • missing files
  • wrong directories

However – obviously at times when I build the solution, there's no problem.

FYI, I uninstalled ReSharper 5.1.1 two weeks ago and Visual Studio's been giving me some errors since then (among them not being able to debug). I re-installed Visual Studio and it's working better since then, but still get this problem. Could it have to do with some ReSharper stuff being somewhere?

Have you had the same problem and solved it? Or do you have any possible solution to it?

Best Answer

While /C may ignore errors, it might not be the real solution as there could be files that MUST be copied in order for the build to be successful.

The most common issue is the missing quotes around the pre-defined command tags (such as $TargetDir). When one creates various branches and paths in code or TFS, there is a very high chance for this to occur.

Sometimes if the file is read only, it will cause issues too. Add the /R option to allow read only files to be copied over. You can find list of available options at:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true

Another possible issue is that the underlying folder cannot be accessed. If so, trying performing "start xcopy" instead of "xcopy". This will open another command window but with admin priveleges.