Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.


I've invariably found this to be a file locking issue. Code 4 is Cannot Access File. One partial solution I found is to use the /C option for xcopy (which continues on error). Not really a solution but mostly it has stopped my builds from failing.

Another solution which only works on 32 bit is to use the unlocker tool to release the windows handles on the file before the copy.

Edit: I've just realised that it works under 64 bits too.


I crossed the same error, but it is not due to the file is locked, but the file is missing.

The reason why VS tried to copy an not existing file, is because of the Post-build event command.

After I cleared that, problem solved.

UPDATE:

As @rhughes commented:

The real issue is how to get the command here to work, rather than to remove it.

and he is absolutely right.

enter image description here


I have also faced this problem.Double check the result in the error window.

In my case, a tailing \ was crashing xcopy (as I was using $(TargetDir)). In my case $(SolutionDir)..\bin. If you're using any other output, this needs to be adjusted.

Also note that start xcopy does not fix it, if the error is gone after compiling. It might have just been suppressed by the command line and no file has actually been copied!

You can btw manually execute your xcopy commands in a command shell. You will get more details when executing them there, pointing you in the right direction.


In case the post build event contains copy/xcopy command for copying build output to some directory(which usually is the most common post build operation) the problem can occur in case the full directory path either of source or target destinations contain folder names which include spaces. Remove space for the directory name(s) and try.


As mentioned in many sites, there are various reasons for this. For me it was due to the length of Source and Destination (Path length). I tried xcopy in the command prompt and I was unable to type the complete source and path (after some characters it wont allow you to type). I then reduced the path length and was able to run. Hope this helps.