Monday, August 27, 2007

Mirror files with robocopy

Robocopy is a built-in function in Vista that allows copying of the contents of one folder to another folder, either within the same computer or over a network. In my case I use it to mirror the contents of my Matlab work folder to a second hard drive. The second hard drive is then backed up to an external server once a day. You can run robocopy as a scheduled task to automate the backup process as often as you like with minimal effort on your part.

To start with, you create a new text file. In it, enter the following text:

@Echo off
robocopy c:\MATLAB701\work\ f:\Luke_stuff\Miller_projects\Matlab_work_copy /MIR /XD c:\MATLAB701\work\Littorine_heat_budget\Run_output_files\

Then save the text file, and change its extension from .txt to .bat (You may need to go to Tools>Folder Options and click the View tab, then unclick "Hide extension for known file types" so that you can alter the .txt to .bat). This lets Windows recognize it as a batch file.

The basic syntax is:
robocopy source destination switches

In the case above, my source directory is c:\MATLAB701\work\ (put quotes " " around it if you use folder names with spaces in them), the destination directory is on my F: drive f:\Luke_stuff\Miller_projects\Matlab_work_copy. The switches used are /MIR, which mirrors the contents of the source directory to the destination directory, including copying all the subfolders and their contents. This will also erase files in the destination directory that are no longer in the source directory. The /XD switch tells robocopy to ignore the contents of the specified directory, in this case c:\MATLAB701\work\Littorine_heat_budget\Run_output_files\ since I don't want the contents of that directory copied over (for space-saving reasons).

To automate the copying process, you can use the Task Scheduler to launch your batch file at specified times. The Task Scheduler is normally found in the Start menu under Programs>Admnistrative Tools>Task Scheduler. Once open, you create a new task, and give it a name, a time to run etc. Under Action, choose "start a program", and under "Program/script:" choose the browse button and find the .bat file that you wrote earlier. You won't need to add any arguments or "start in" stuff, just hit next and finish.