{"id":17,"date":"2016-02-12T09:46:18","date_gmt":"2016-02-12T17:46:18","guid":{"rendered":"http:\/\/www.timmevans.net\/blog\/?p=17"},"modified":"2016-02-12T09:46:18","modified_gmt":"2016-02-12T17:46:18","slug":"decoding-robocopy-exit-codes","status":"publish","type":"post","link":"https:\/\/www.timmevans.net\/blog\/decoding-robocopy-exit-codes\/","title":{"rendered":"Decoding Robocopy Exit Codes"},"content":{"rendered":"<p>Sometimes, you write a script that uses robocopy to copy or move files around efficiently, and you need to confirm that no errors occurred during robocopy\u2019s run. Unfortunately, robocopy doesn\u2019t use the standard error return mechanism of 0 for no errors and 1 (or some other non-zero number) if an error occurred. Microsoft documents robocopy exit codes in <a href=\"https:\/\/support.microsoft.com\/en-us\/kb\/954404\" target=\"_blank\">KB 954404<\/a>, but the information isn\u2019t complete. A better explanation can be found in a <a href=\"http:\/\/blogs.technet.com\/b\/deploymentguys\/archive\/2008\/06\/16\/robocopy-exit-codes.aspx\" target=\"_blank\">Deployment Guys blog post<\/a> and shows that the exit codes are bitmapped:<\/p>\n<table width=\"611\">\n<tbody>\n<tr>\n<td width=\"59\"><strong>Code<\/strong><\/td>\n<td width=\"552\"><strong>Meaning<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"59\">0<\/td>\n<td width=\"552\">No errors occurred and no files were copied.<\/td>\n<\/tr>\n<tr>\n<td width=\"59\">1<\/td>\n<td width=\"552\">One of more files were copied successfully.<\/td>\n<\/tr>\n<tr>\n<td width=\"59\">2<\/td>\n<td width=\"552\">Extra files or directories were detected.\u00a0 Examine the log file for more information.<\/td>\n<\/tr>\n<tr>\n<td width=\"59\">4<\/td>\n<td width=\"552\">Mismatched files or directories were detected.\u00a0 Examine the log file for more information.<\/td>\n<\/tr>\n<tr>\n<td width=\"59\">8<\/td>\n<td width=\"552\">Some files or directories could not be copied and the retry limit was exceeded.<\/td>\n<\/tr>\n<tr>\n<td width=\"59\">16<\/td>\n<td width=\"552\">Robocopy did not copy any files.\u00a0 Check the command line parameters and verify that Robocopy has enough rights to write to the destination folder.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>These codes are combined to give a complete indication of the results of the run. For example, if the exit code is 3, that indicates that files were copied successfully and that extra files or directories were detected (1 + 2 = 3). The important conclusion is to note that any return code with 0x8 or 0x16 set indicates that some or all of the files were not copied.<\/p>\n<p>To decode these exit codes in Powershell, use the bitwise and operator <strong>\u2013band<\/strong> to test for the bits we are interested in. In binary, the important bits are 011000, which converts to decimal 24. So, you can do your robocopy operation like this:<\/p>\n<pre>$cmd_args = @($source, $target, $action, $options)\r\n&amp; robocopy.exe @cmd_args\r\nIf ($LastExitCode -band 24) {\r\n    Write-Host \u201cEverything is OK\u201d\r\n } else {\r\n    Write-Host \u201cErrors happened\u201d\r\n}<\/pre>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, you write a script that uses robocopy to copy or move files around efficiently, and you need to confirm that no errors occurred during robocopy\u2019s run. Unfortunately, robocopy doesn\u2019t use the standard error return mechanism of 0 for no errors and 1 (or some other non-zero number) if an error occurred. Microsoft documents robocopy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":4,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":21,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/21"}],"wp:attachment":[{"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.timmevans.net\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}