如何真正理解robocopy返回码2

问题描述 投票:0回答:2

我正在努力解决如何处理有时在

robocopy
期间返回2的部署脚本。命令和输出如下。

它返回2,这意味着“额外的文件”。

总体而言,这似乎是成功的。我应该接受 2 作为成功吗?

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

   ROBOCOPY :: 适用于 Windows 的强大文件复制
-------------------------------------------------- ----------------------------

  开始时间 : 2017 年 1 月 24 日星期二 11:53:40 PM
   来源:C:\Download\Temp\
     目标:C:\Inetpub\

    文件:*.*

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

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

      *额外文件 689351 C:\Inetpub\admin\dynamicfile2.zip

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

               已跳过的复制总数 不匹配 失败 附加内容
    目录 : 510 510 0 0 0 0
   文件:3564 0 3564 0 0 1
   字节 : 606.15 m 0 606.13 m 0 0 673.1 k
   时间 : 0:00:00 0:00:00 0:00:00 0:00:00
   结束 : 2017 年 1 月 24 日星期二 11:53:40 PM
powershell robocopy
2个回答
10
投票

如有疑问,请阅读文档(尽管在这种情况下SS64可能是更好的来源):

下表列出并描述了 Robocopy 实用程序使用的返回代码。

0 未复制任何文件。没有遇到失败。没有文件不匹配。文件已存在于目标目录中;因此,复制操作被跳过。
1 所有文件均已成功复制。
2 目标目录中存在一些源目录中不存在的其他文件。没有复制任何文件。
3 复制了一些文件。存在其他文件。没有遇到失败。
5 复制了一些文件。一些文件不匹配。没有遇到失败。
6 存在其他文件和不匹配的文件。没有复制任何文件,也没有遇到任何故障。这意味着文件已存在于目标目录中。
7 文件已复制,存在文件不匹配,并且存在其他文件。
8 多个文件未复制。

注意 任何大于 8 的值都表示复制操作期间至少有一次失败。

退出代码最多(含 7)表示无错误操作状态。 8 及以上的退出代码表示错误。

就像 Mathias 已经说过的那样,您是否愿意将额外文件视为错误取决于您。如果您不关心额外或不匹配的文件/文件夹,只需检查退出代码是否大于 7:

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

否则检查退出代码是否大于 1:

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

0
投票

Robocopy 返回代码的完整豪华显示
阅读本文档以获取 Robocopy 的详细说明
我通过这个问题

找到了该文档
call :screencolors
Robocopy.exe "src" "dest" /s  
call :robocopy_check %errorlevel%
pause
exit

:robocopy_check 
set "space=            " 
if "%1"=="16" ( echo.%_fBRed%%space%***FATAL ERROR***%_fLGray% 
                goto :end_of_check )  
if "%1"=="15" ( echo.%_fBRed%%space%**FAILED COPIES**    
                echo.%space%*MISMATCHES*  
                echo.%space%EXTRA FILES  
                echo.%space%Copy successful%_fLGray% 
                     goto :end_of_check )  
if "%1"=="14" ( echo.%_fBRed%%space%**FAILED COPIES**    
                echo.%space%*MISMATCHES*  
                echo.%space%EXTRA FILES%_fLGray% 
                     goto :end_of_check )  
if "%1"=="13" ( echo.%_fBRed%%space%**FAILED COPIES**    
                echo.%space%*MISMATCHES*  
                echo.%space%Copy successful%_fLGray% 
                     goto :end_of_check )  
if "%1"=="12" ( echo.%_fBRed%%space%**FAILED COPIES**    
                echo.%space%*MISMATCHES*%_fLGray% 
                     goto :end_of_check )  
if "%1"=="11" ( echo.%_fBRed%%space%**FAILED COPIES**    
                echo.%space%EXTRA FILES  
                echo.%space%Copy successful%_fLGray% 
                     goto :end_of_check )  
if "%1"=="10" ( echo.%_fBRed%%space%**FAILED COPIES**    
                echo.%space%EXTRA FILES%_fLGray% 
                     goto :end_of_check )  
if "%1"=="9"  ( echo.%_fBRed%%space%**FAILED COPIES**    
                echo.%space%Copy successful%_fLGray% 
                     goto :end_of_check )  
if "%1"=="8"  ( echo.%_fBRed%%space%**FAILED COPIES**%_fLGray% 
                goto :end_of_check )  
if "%1"=="7"  ( echo.%space%*MISMATCHES*  
                echo.%space%EXTRA FILES  
                echo.%space%Copy successful 
                     goto :end_of_check )  
if "%1"=="6"  ( echo.%space%*MISMATCHES*  
                echo.%space%EXTRA FILES 
                     goto :end_of_check )  
if "%1"=="5"  ( echo.%space%*MISMATCHES*  
                echo.%space%Copy successful 
                     goto :end_of_check )  
if "%1"=="4"  ( echo.%space%*MISMATCHES* 
                goto :end_of_check )  
if "%1"=="3"  ( echo.%space%EXTRA FILES  
                echo.%space%Copy successful 
                     goto :end_of_check )  
if "%1"=="2"  ( echo.%space%EXTRA FILES 
                goto :end_of_check )  
if "%1"=="1"  ( echo.%space%Copy successful 
                goto :end_of_check )  
if "%1"=="0"  ( echo.%space%--No change-- 
                goto :end_of_check )  
:end_of_check 
exit /b  
      
:robocopy_error  
echo. 
echo.FATAL ERROR        Robocopy did not copy any files. This is either a usage error or an error due to insufficient access privileges on the source or destination directories.
echo.FAILED COPIES      Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.
echo.MISMATCHES     Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.
echo.EXTRA FILES        Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.
echo.Copy successful        One or more files were copied successfully (that is, new files have arrived).
echo.no change      No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.
echo. 
exit /b   

  
rem ***********************************************************************************************
rem *** ANSI codes for screen color, cursor position 
rem ***********************************************************************************************
:screencolors  
rem source https://ss64.com/nt/syntax-ansi.html
rem Define foreground and background ANSI colors:
rem B means Bright
set "_fBlack=[30m" 
set "_bBlack=[40m"
set "_fRed=[31m"
set "_bRed=[41m"
set "_fGreen=[32m"
set "_bGreen=[42m"
set "_fYellow=[33m"
set "_bYellow=[43m"
set "_fBlue=[34m"
set "_bBlue=[44m"
set "_fMag=[35m"
set "_bMag=[45m"
set "_fCyan=[36m"
set "_bCyan=[46m"
set "_fLGray=[37m"
set "_bLGray=[47m"
set "_fDGray=[90m"
set "_bDGray=[100m"
set "_fBRed=[91m"
set "_bBRed=[101m"
set "_fBGreen=[92m"
set "_bBGreen=[102m"
set "_fBYellow=[93m"
set "_bBYellow=[103m"
set "_fBBlue=[94m"
set "_bBBlue=[104m"
set "_fBMag=[95m"
set "_bBMag=[105m"
set "_fBCyan=[96m"
set "_bBCyan=[106m"
set "_fBWhite=[97m"
set "_bBWhite=[107m"
set "_Bold=[1m"
set "_Underline=[4m"
set "_NoUnderline=[24m"
set "_Reverse=[7m"
set "_NotReverse=[27m"
set "_RESET=[0m"             & rem Default
exit /b 
© www.soinside.com 2019 - 2024. All rights reserved.