Friday, June 29, 2007

Robocopy

One other utility that Vista comes with which is way better than previous Windows is the robocopy utility.  It's got a lot of options and features, but one that I find very useful is the mirroring option.

With the mirroring option you can create exact copy of the source, meaning that if there are files in the source that are different from the destination, they will be copied, if there are extra files already in the destination that don't exist in the source, they will be deleted.

robocopy /MIR source destination

Try it out.  Play with it.  You'll like it.

One thing you want to watch out about robocopy however, is the error code it returns when it exits.  It's not the straightforward zero == success and non-zero == fail.  Oh, no.  That would be too easy.  So here is a list of the various error codes that robocopy gives out.

Error code Meaning
0 No change
1 Files were copies successfully
2 Extra files deleted from destination
4 Some mismatched files detected
8 Some files could not be copied
16 Fatal error

 

The error codes form a bit field, though.  So 3 really means 2+1, meaning there are extra files but copy was performed successfully.  That means actual failure is anything greater than or equal to error code 8.

robocopy /MIR source destination

if %errorlevel% geq 8 echo FAIL!

Once you know that, it's a sweet utility to have around.  If you're not on Vista, it is included in the Win2k3 Resource Kit Tools which you can download.

2 comments:

ef said...

It seems that Robocopy XP026 (Robocopy GUI version) always returns 0 as %ErrorLevel%, so you can't check copy errors on Xp/2k3. Any trick to fix this bug? Thanks

Brictone said...

Hey Awesome site, I just did a backup batch file with xcopy. I think I'm going to update it with robocopy.

I'm always looking for ways to automate stuff and make life easier, and I know nothing about programing and scripting so this is very helpful.

Thanks!