Wednesday, November 18, 2009

XCopy is failing on you? Use Robocopy!

I recently had an issue when trying to backup my data from the laptop to another computer.  I was using XCopy, as usual, to copy all the changed files from a folder and all its sub-folders, so that I keep a mirror of my personal folder on a remote location: all the files that have the 'archive' flag set in this folder (or one of its sub-folders) are copied, and then their flag is reset.  Any modification of those files will automatically set the flag, and the files will thus be copied during the next sync.

Recently, all my attempts to use Xcopy on this particular folder were failing with an 'Insufficient memory' error.  On a laptop with 4 GB of RAM! It turns out that XCopy still has a limitation from the DOS era: it can't handle full pathnames (location + filename) that are longer than 256 chars! Blah!

A bit of googling found a possible solution, that turned out to be even more useful than XCOPY, namely Robocopy.  This tool is included into Windows Vista. It was originally part of the Windows Server 2003 Resource Kit, but you actually only need the 'robocopy.exe' file, that can be copied onto any Windows NT or Windows XP machine and does not need any installation.

This tool has options that are the same as the ones from XCopy, and thus allows everything that's possible with the latter.  But it can also do much more :
  • It allows very fine-grained choices of which files to copy, chosen depending on e.g. their attributes;
  • It knows about the permissions and security access lists attached to the files;
  • It can move the files, instead of simply copying them;
  • It can remove files in the destination that are not any more in the source folder ('Mirror' feature).
This last feature is the one that convinced me.  I can now freely move files around without wondering about the fact that I should delete them from their original location in my mirror.

Nice tool!