Syncing SharePoint Document Libraries with the File Share

Recently a client with several hundred SharePoint subsites requested a solution for syncing folders from their file server to their SharePoint server--they wanted to maintain their current file shares, and simply have any files saved to them sync one way to the appropriate subsites.  To facilitate this, we decided to use robocopy.exe, a tool included in Windows Vista, Windows 7, and Windows Server 2008 (also available for Windows XP and Windows Server 2003 via free Windows Resource Kits from Microsoft). 

robocopy.exe is ideal in situations where you need to replicate the contents of one folder to another often.  To invoke it, simply type robocopy in the command-line, and a few notes about the tool will come up.  But to actually start copying, you will need to use the following syntax:

robocopy “[source]” “[destination]” /[switches]

The [source] is the full path name of the folder you want copied, while [destination] is the full path name of the folder you are copying to.  [switches] can be replaced with a number of parameters, all preceded by a forward slash, that specify exactly how you want your files and folders copied.  A complete list of robocopy’s switches can be obtained by entering robocopy /? in the command-line, but we used the following:

robocopy "[source]" "[destination]" /MIR /V /NP /R:10 /W:30 /XF thumbs.db

where /MIR directs robocopy to mirror the contents and structure of the source to the destination (note:   if files are deleted from the source, the /MIR switch will delete them from the destination); /V logs skipped files; /NP suppresses progress displays in the command-line; /R:10 specifies that if a file fails to copy, it should be retried 10 times; /W:30 means wait 30 seconds between retries; and /XF excludes files to be copied (we excluded the thumbs.db files that were present in all their folders).

Once composed, you can use this command to mirror the contents of one folder to another—in essence, one-way syncing them.  But what if you have more than one set of folders, and what if you want to run the command often?  Well the next thing we did was compile the separate robocopy commands for all our client’s folders in Notepad, and saved them as one .bat, or batch, file.  Then by clicking the .bat file, all the commands it contains will automatically run, and rerunning them is just a matter of creating a scheduled task for the file.

If you are interested in using robocopy, but are not familiar with the command-line, see the robocopy GUI application from Microsoft TechNet:  http://technet.microsoft.com/en-us/magazine/2006.11.utilityspotlight.aspx.