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

Solution 1:

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.

Solution 2:

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.