The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Wednesday, June 1, 2011

robocopy fix for exit code of 2

Sometimes you want to use robocopy from nant, a sql job or another build tool, robocopy will return 2 during a successful run, this gets interpreted as an error, a google search on the issue returned this article Fixing Robocopy for SQL Server Jobs.

Robocopy is now installed by default on Windows7 and Windows Server 2008
http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx

here is robocopy_fix.bat based on the code in the "Fixing Robocopy for SQL Server Jobs" article

rem http://weblogs.sqlteam.com/robv/archive/2010/02/17/61106.aspx
robocopy %*
rem suppress successful robocopy exit statuses, only report genuine errors (bitmask 16 and 8 settings)
set/A errlev="%ERRORLEVEL% & 24"
rem exit batch file with errorlevel so SQL job can succeed or fail appropriately
exit/B %errlev%
rem USAGE: just like regular robocopy
robocopy_fix.bat C:\temp\s C:\temp\d /MIR

No comments: