1
votes

I am struggling with how to handle a deploy script that sometimes returns a 2 during robocopy. The command and output are below.

It returns 2, which means "extra file".

It appears, overall, to be a success. Should I just accept 2 as being success?

-------------------------------------------------------------------------------

   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Tuesday, January 24, 2017 11:53:40 PM
   Source : C:\Download\Temp\\
     Dest : C:\Inetpub\\

    Files : *.*

  Options : *.* /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /MT:8 /R:1000000 /W:30 

------------------------------------------------------------------------------

      *EXTRA File         689351    C:\Inetpub\\admin\dynamicfile2.zip

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       510       510         0         0         0         0
   Files :      3564         0      3564         0         0         1
   Bytes :  606.15 m         0  606.13 m         0         0   673.1 k
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Tuesday, January 24, 2017 11:53:40 PM
1
I'm having trouble interpreting your options. /S and /E are mutually exclusive?! The /E and PURGE are implied by the /MIR. DCOPY doesn't have a DA flag?! If you intent is to mirror source and destination, a return code 2 (extra file) should be an error. - Lieven Keersmaekers
It's up to you whether you want to accept that extra files are present in the destination folder - Mathias R. Jessen

1 Answers

6
votes

When in doubt, read the documentation (although in this case SS64 might be a better source):

The following table lists and describes the return codes that are used by the Robocopy utility.

0 No files were copied. No failure was encountered. No files were mismatched. The files already exist in the destination directory; therefore, the copy operation was skipped.
1 All files were copied successfully.
2 There are some additional files in the destination directory that are not present in the source directory. No files were copied.
3 Some files were copied. Additional files were present. No failure was encountered.
5 Some files were copied. Some files were mismatched. No failure was encountered.
6 Additional files and mismatched files exist. No files were copied and no failures were encountered. This means that the files already exist in the destination directory.
7 Files were copied, a file mismatch was present, and additional files were present.
8 Several files did not copy.

Note Any value greater than 8 indicates that there was at least one failure during the copy operation.

Exit codes up to and including 7 indicate non-error operational status. Exit codes of 8 and above indicate errors.

Like Mathias already said, whether you do or don't want to treat extra files as an error is up to you. If you don't care about extra or mismatched files/folders just check for exit codes greater than 7:

& robocopy ...
if ($LastExitCode -gt 7) {
    # an error occurred
    exit $LastExitCode
}
exit 0

otherwise check for exit codes greater than 1:

& robocopy ...
if ($LastExitCode -gt 1) {
    # an error occurred
    exit $LastExitCode
}
exit 0