Robocopy fails when is used from TFS Builds

RoboCopy has ExitCodes > 0.

In your example Exit Code = 1 means One or more files were copied successfully (that is, new files have arrived).


To fix this you could create a Powershell Script, which executes the copy and overwrites the Exit code.

like

param( [String] $sourcesDirectory, [String] $destinationDirectory, [String] $attributes)

robocopy $sourcesDirectory $destinationDirectory $attributes

if( $LASTEXITCODE -ge 8 )
{
    throw ("An error occured while copying. [RoboCopyCode: $($LASTEXITCODE)]")
}
else
{
    $global:LASTEXITCODE = 0;
}

exit 0

robocopy use the error code different, error code 1 is not a real error, it just saying that one or more files were copied successfully.

TFS recognize error code 1 as a real error and fail the build.

To solve that you need to change the robocopy error code:

(robocopy c:\dirA c:\dirB *.*) ^& IF %ERRORLEVEL% LEQ 1 exit 0

The ^& IF %ERRORLEVEL% LEQ 1 exit 0 convert the error code 1 to 0 and then the TFS build will not be failed.