Back to Technology
Batch File Using Robocopy To Keep 2 Directories In Sync

By: Gordon Rankine
Published: 04 Apr 2014 00:01

Often users will have an external USB drive that they will manually copy files from their PC to, they do this so they can have a backup copy of their data. This can be a time consuming task and more often than not, files and not copied over as they are missed by the user.

If you are looking to set up an easier way to keep your backup data in line with your PC data then follow the steps below. This is a simple batch file that copies files and folder from one directory to another. Also only files that have changed since the batch file was last run will be copied over.

1. If you are using anything earlier than Windows 7 or Server 2008 then you will probably have to download and install Robocopy as it is not included with earlier operating systems. It can be downloaded as part of the Windows Server 2003 Resource Tools by clicking on this link.

2. Hold Start and press R at the same time, type cmd then press enter. This will open up the windows command line interface.

3. Type robocopy /? and press enter. You should see a lot of information flash up, this means robocopy is working on your PC. If you see the message robocopy is not recognised as an internal or external command, operable program or batch file, then it is not installed on your PC. Go back to step 1. The command prompt window can be closed by typing exit then enter or by clicking the cross at the top right of the window.

4. Open up notepad on your PC.

5. Copy and paste the code below.

6. Replace "C:\Users\Gordie\Music\My MP3s" with the directory that you wish to backup (the source).

7. Replace "G:\mp3s backups" with the directory that you wish to backup to (the destination).

8. Save the file as Backup.bat or similar. Ensure that you change the save as type to All Files (*.*), this will allow you to save the notepad document as a batch file.

9. Run the batch file whenever you want to take a backup of your data. A summary will be display once the script has finished running, press enter to close the script after reviewing the summary. If you do not want to see the summary remove the line pause from the script.

Please note that this batch file will mirror exactly what files and folders are in the source directory. IF YOU HAVE MOVED ANY FILES MANUALLY INTO THE DESTINATION FOLDER AND THEY ARE NOT PRESENT IN THE SOURCE DIRECTORY THEY WILL BE DELETED.

I suggest that you try this out on some test folders first before you backup your real data.

@echo off
Title MP3 Sync Process
robocopy "C:\Users\Gordie\Music\My MP3s" "G:\mp3s backups" /MIR

Disclaimer: This script is provided as is without any warranty. You are responsible for running this script and I will not be held accountable for any data loss or issues arising from this script.

Add Comment

Batch Files   Backups   Windows   Scripts

Back to Technology