Standard Windows Cli Tools
Table of Contents
- R
robocopy
Overview
robocopy
is the modern, recommended way to do copying (of folders or many files) on DOS/Windows. The syntax etc seem quite straight-forward for long-time users of DOS and Windows sheels. But there is a catch, the used return values.
While standard DOS/Windows cli programs generally return 0
to denote a successful run, and all other numbers represent some kind of error condition, robocopy
denotes also some success results with non-zero return values.
Some relevant switches
Switch | Description |
---|---|
/S |
Copy subfolders, but no empty subfolders. |
/E |
Copy all subfolders, including empty ones. |
/LOG:Filename |
Write a log to Filename (overwriting previous log content). |
/LOG+:Filename |
Write a log to Filename (appending to the existing log content). |
Exit codes
The basic exit codes are the powers of 2, but some combinations of these are used as well:
Exit Code | Meaning | Severity | More info |
---|---|---|---|
0 | No files were copied. No failure. | ✅ OK | The source and destination directory trees are completely synchronized. |
1 | Some files were copied. No failure. | ✅ OK | i.e. new files have arrived |
2 | Extra files or directories were detected. | ✅ OK | No files were copied, examine the output log for details. |
3 (1+2) | Files were copied and extra files detected. | ✅ OK | No failure was encountered. |
4 | Some Mismatched files or directories were detected. | ⚠️ Warning | Examine the output log. Housekeeping might be required. |
5 (1+4) | Some files were copied. Some files were mismatched. | ⚠️ Warning | No failure was encountered. Examine the output log. Housekeeping might be required. |
6 (2+4) | Additional files and mismatched files exist. | ⚠️ Warning | No files were copied and no failures were encountered. This means that the files already exist in the destination directory. |
7 (1+2+4) | Files were copied, a file mismatch was present, and additional files were present. | ⚠️ Warning | |
8 | Some files or directories could not be copied. | ❌ Error | Copy errors occurred and the retry limit was exceeded. Check these errors further. |
16 | Serious error. Robocopy did not copy any files. | ❌ Error | Either a usage error or an error due to insufficient access privileges on the source or destination directories. |
(reference for this table: https://ss64.com/nt/robocopy-exit.html)
Report detailled errors in batch file
if %ERRORLEVEL% EQU 16 echo ***FATAL ERROR*** & goto end
if %ERRORLEVEL% EQU 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto end
if %ERRORLEVEL% EQU 14 echo FAIL + MISMATCHES + XTRA & goto end
if %ERRORLEVEL% EQU 13 echo OKCOPY + FAIL + MISMATCHES & goto end
if %ERRORLEVEL% EQU 12 echo FAIL + MISMATCHES & goto end
if %ERRORLEVEL% EQU 11 echo OKCOPY + FAIL + XTRA & goto end
if %ERRORLEVEL% EQU 10 echo FAIL + XTRA & goto end
if %ERRORLEVEL% EQU 9 echo OKCOPY + FAIL & goto end
if %ERRORLEVEL% EQU 8 echo FAIL & goto end
if %ERRORLEVEL% EQU 7 echo OKCOPY + MISMATCHES + XTRA & goto end
if %ERRORLEVEL% EQU 6 echo MISMATCHES + XTRA & goto end
if %ERRORLEVEL% EQU 5 echo OKCOPY + MISMATCHES & goto end
if %ERRORLEVEL% EQU 4 echo MISMATCHES & goto end
if %ERRORLEVEL% EQU 3 echo OKCOPY + XTRA & goto end
if %ERRORLEVEL% EQU 2 echo XTRA & goto end
if %ERRORLEVEL% EQU 1 echo OKCOPY & goto end
if %ERRORLEVEL% EQU 0 echo No Change & goto end
:end
Recommended fix for Visual Studio Build Actions
This “fix” is required, because by default (just using the robocopy command directly) it is quite probable that a non-zero exit code is the result, and this is interpreted as error by the build process, thereby failing.
robocopy "$(ProjectDir)Docs" "$(OutDir)Docs" /E
IF %ERRORLEVEL% LEQ 3 (
EXIT 0
) ELSE (
EXIT %ERRORLEVEL%
)