How-To: Synchronise two folders using xcopy.

Difficulty: 3

SyncThe main reason you may want to sync folders on a single machine is to backup your data. You would probably be best to find a backup utility to do this.

If you want to sync folders over networked drives, this guide may help you also. There are more detailed articles at Inspect My Gadget on syncing documents between two networked computers. Find them here.

On the very odd occasion you do want to sync folders on a single computer, it is not too hard to achieve. There are  number of applications available to help you do this, but many of them cost money. Windows has a solution by a command in the command prompt which will work on all Windows PC’s. Microsoft also offers a program called SyncToy which does the same thing with a nice GUI interface.

This is a two part article. Part 1 will look at using xcopy to sync your folders, and Part 2 will look at using SyncToy.

Part 1 will cover the following topics.

  • How to use xcopy
  • How to put xcopy into a batch file
  • How to schedule the batch file to run a sync at a set time.

The reason I like xcopy is that it is quick to setup, and you do not require any additional software to get it to work. This means it is good for 1 off syncs. SyncToy will be the best solution for most people who require to sync more frequently.

This two part article was originally going to be much shorter, but I got typing and my fingers wouldn’t shut up. I recommend that beginners read the entire article to prevent disaster and to get all the extra information. Advanced users will probably be able to skim through most of it, wary that a typing error may result in lost data.

I am generally a big fan of off-line files for smaller syncs over networked drives. This does not work with two folder on a single computer, so we have to look at other methods. The advantage of off-line files is that the data on both computers, while the computers are connected, will be the same even if a sync has not taken place recently.

With the methods in this article, the folders will only be the same after a sync which you have scheduled to run, or by running the sync manually. I am not aware of any applications which will recognise changes as they are made and force a sync at that time.

ExclimationIt is very important before you play with this, that you backup your data as you may need to do a little tweaking to get it working right for you. You may overwrite files with wrong versions if you make a typo. This is particularly important with xcopy. The easiest way to backup your data is to use Windows File Explorer to copy your files to a safe place. I can’t be held responsible for lost data. Do this at your own risk.

A bit of information about XCOPY:

Xcopy is an MS-DOS command which expands on the normal copy command, giving you a thorough list of switches to help you get it to work just as you want. The advantages of the xcopy command over the copy command is that it lets you copy only updated files, by a entering a series of switches. The copy command will copy the whole directory and prompt you if files need to be overwritten.

For most backup purposes, you only need to go one way. One of your folders will be a work folder(source) and another will be the backup folder(destination). As new files will not be created in the backup folder, you do not have to sync the backup folder back to the source folder. If you do need to Sync both ways, just run the command twice, switching the source and destination folders the second time. This will be covered further in this article.

Play with XCOPY

The best place to start is to open up a command prompt by clicking on “Start”, selecting “Run”, and typing in “cmd” followed by enter. If you are using Vista, you can type “cmd” directly into the search box followed by enter. Either way, you should end up with a command prompt.

type “xcopy /?” followed by enter

This will show you all of the switches you can play around with. I will be using the /D  and /E switches only in this article. You can feel free to play with the others, though I have not needed them myself. Just take caution, this is where you can stuff up your data if you have not backed up.

The /D switch copies all the files from your source to your destination, unless a file at your destination is newer than the source. If there is a newer file at the destination, that file will be skipped and the next file will be checked. The copying of a file will only take place if the destination file does not yet exist or is older than the source.

The /E switch tells xcopy to include folders and subfolders. If you do not include this, only the files in the root of the folder will be copied. Worth noting is /S which will copy all folders across apart from empty folders. /S can be used instead of /E

ExclimationThere will NOT be any prompts asking you if you want to overwrite files using only the /D switch. Xcopy will overwrite the file in the destination if the source file is newer. There is no easy way to recover the file in XP or Vista. If you are a Vista user, you may get lucky by using the “Previous Version” functions in folders properties.

Sync with XCOPY:

The command “xcopy folder1 folder2 /D /E” will copy all the folders and files from folder1 to folder2. If a file exists in folder 2 that is newer or the same as a file in folder 1, the file will not be overwritten.

So to do it both ways, you can do as above and then the command “xcopy folder2 folder1 /D /E”. This will copy any folders and files that are new or don’t exist in folder1 across to folder1.

Both folders will now be the same.

Setting up a batch file:

You can place batch files anywhere on your computer, but for once I will recommend putting it in the root of your C: drive. If you want to create multiple files, create a folder called “sync” or “batch” to keep it tidy.

Open up Notepad, it’s in you accessories folder in the start menu.

You don’t need to make the batch file very complicated. If you have multiple syncs, you can put them all in the once batch file.

Start small with some test folders to be safe. Please don’t start off syncing your photos or music on your first run.

Put in the following commands, on separate lines, replacing folder 1 & 2 for your own folder and path names.

xcopy C:\folder1 C:\folder2 /D /E
xcopy C:\folder2 C:\folder1 /D /E

Save your file as sync.bat. You will need ensure you don’t save it as a txt file by selecting all files in the filetype dropdown.

You can test this out now, by double clicking on the batch file. You should then be able to prove it has worked by both folders containing the same files.

Test it some more by putting a text file in folder and then syncing. Check that the file is copied across to both. Try updating both text files and then syncing your folders. The most recently updated text file should be able to be found on both, with the older of the two being lost forever.

Scheduling your batch file:

It is nice to think that we will backup our data regularly on our own accord, but if you are like the other 4 billion or so computer users in this world, you would find it best to schedule it so you don’t need to check up on it again.

This is slightly different between XP and Vista, but the theory is the same so I will run through how to do this in XP. Vista calls their scheduler “Task Scheduler”, where as XP calls its scheduler “Scheduled Tasks”. Both can be found by clicking “Start” -> “Accessories” -> “System Tools”.

Click “Add Scheduled Task”
Click “Next”
Click “Browse” and navigate to your batch file
Select how frequently you would like it to happen. If your computer is on all the time, 3am is ideal. If you don’t have your computer on much, make it happen as your computer starts. This is completely up to you.
Follow the date prompts and that is it.

You are still able to launch the file manually if you require it to happen instantly, but otherwise, your folders will always be synced at the scheduled time.

Please check out part two of this article when it is available (Monday), which covers SyncToy so you can compare the two methods and figure out which you would prefer to use. I hadn’t played with xcopy much before writing this article, and I am thinking I may well end up using it over SyncToy. I feel like there is more control this way.

I hope you have enjoyed this article and found it useful. Please stick around and check out some other articles at Inspect My Gadget.

Related Posts

16 comments so far »

  1. xcopy said, on October 12, 2007 @ 11:55 pm

    the method works fine as long as you don’t want to delete a file from one of the folders. (or you have to delete it from both folders)

  2. Inspect My Gadget said, on October 13, 2007 @ 7:56 am

    Good point. I hadn’t twigged to this as I was writing the article. Thanks for letting me know.

  3. Michael said, on December 26, 2007 @ 1:30 pm

    Your explanation of the /D switch was very useful to me. Thanks

  4. jan welmers said, on July 1, 2008 @ 4:49 am

    if you use foldernames with a space in it then like C:\Document and Setting than you should put quotes around the source and/or the destination part of the xcopy command

    example use:
    xcopy “C:\Document and Setting\*.*” D:\BACKUP\ /i/s
    in stead of:
    xcopy C:\Document and Setting\*.* D:\BACKUP\ /i/s

  5. Tom said, on December 2, 2008 @ 6:54 am

    Can XCOPY be used to copy to multiple destinations simultaneously, e.g. a networked computer and a a Network Attached Storage Device? If so, what is the syntax?

  6. Chris Duckworth said, on December 2, 2008 @ 7:29 am

    Hi Tom, you would need to put them in a batch file. Create a text document with a .cmd extension. Then in that file, put all your xcopy commands to run one after another. To sync over many destinations, you may need to run the script twice.

  7. netclient said, on April 23, 2009 @ 7:12 am

    does xcopy commands consists in the robocopy UI?

  8. prasad said, on April 28, 2009 @ 3:50 pm

    If some files are deleted from source folder is it possible to delete those from destination also? I have source folder synced with destination folder but after sometime some files are deleted from source folder now is it possible to keep the two folders in sync with xcopy?

  9. Chris Duckworth said, on May 6, 2009 @ 10:11 am

    It doesn’t work for deleting files. You may do better using synctoy if you want deleted files to be synced.

  10. a said, on June 16, 2009 @ 1:36 am

    When I run the xcopy /D /E command to backup my email it keeps asking me if I want to Overwrite the file? Why? I thought it wouldn’t do that as long as I used the /D command

    xcopy “C:\Documents and Settings\Administrator\Application Data\Thunderbird” Z:\EmailBackup\Thunderbird\ /D/E

    Z is a mapped network drive. Is that the problem?


  11. Chris Duckworth said, on June 19, 2009 @ 10:08 am

    I think it may be to do with the location of the email file. It is in application data which is a secure place. If thunderbird allows you to change the location of the file, you coul dget around it that way, otherwise check the security settings of the folder. You may need to turn off read only.

  12. LM said, on October 16, 2010 @ 1:39 pm

    I run the following command where d: is in my desktop computer and z: is a mapped network drive.
    xcopy d:\myWork z:\myWork /D /E
    When I run the above command for the first time it copied the source directory partially (703 files) and then stopped. To check the problem, I run the same command again and it kept asking whether to overwrite each of the file in the destination. Since no file was modified, I hoped that I would not get such prompt. Any help would be appreciated. Thanks

  13. Troy said, on November 4, 2010 @ 5:23 am

    You need to use the /Y switch to prevent being prompted to confirm the overwriting of existing files.

  14. Paul Sulwer said, on September 21, 2011 @ 11:59 am

    Thanks for mentioning the /D switch; I didn’t think there was a way to skip overwriting existing files (whether or not they were modified), but this works well. In the real world you would probably want to overwrite older files anyway. I think this is better than using /M to track changes via the archive attribute.
    Xcopy seems terribly slow however, when backing up a large amount of data to an external (USB2) hard drive, but it otherwise seems like the best way to automate an incremental backup.

  15. Paul Sulwer said, on September 21, 2011 @ 12:10 pm

    Forgot to mention the details: I was using Xcopy to back up 20 GB from a local disk to a USB HD. It had been running for maybe 1/2 hour – 1 hour; I was checking something and somehow closed the DOS window! Only about 1/2 of the data was transferred and I sure didn’t want to start over again, and it would have been a pain to try to figure out exactly where it left off then transfer the remaining data manually, so I’m glad I found this page.
    I ran xcopy again with the /D switch and it sort of sat there with no output for at least 10 minutes, during which time I assume it was checking which files needed to be transferred. It finally started copying the remaining files and looks ok so far. Would be nice if there were a faster way to accomplish this though.

Leave a Comment

Name: (Required)

E-mail: (Required but will not be published)