Syncing Files Using The Windows Command Line

To sync files using the Windows command line you will need to use the xcopy command. The default action of this program is to copy a file or directory from one place to another, but you can give it flags to tell it to sync the files. There are a few flags available (use xcopy /? to see them all) but you will probably only want to use the following:

  • /D - Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.
  • /I - If destination does not exist and copying more than one file assumes that destination must be a directory.
  • /E - Copies directories and subdirectories, including empty ones.
  • /Y - Supresses prompting to confirm you want to overwrite and existing destination file.
  • /H - Copies hidden and system files also.
  • /R - Overwrites read only files.

The /F flag is also useful for testing (and probably peace of mind) as it displays full source and destination names while copying.

To sync files from one directory to another you can use xcopy like this:

xcopy c:\mysourcedirectory c:\mydestinationdirectory /D /I /E /F /Y /H /R

To sync with a remote server can just use a server address and directory name as the destination directory:

xcopy c:\mysourcedirectory \\myserver\mydestinationdirectory /D /I /E /F /Y /H /R

If you want to create a temporary local drive for a server directory then you can use the pushd command.

pushd \\myserver\mydestinationdirectory

When you run this command you will be moved to a new directory letter that your system designates. Essentially, it goes backwards through the alphabet to find a drive letter that it can use. You can now treat the network folder as a local drive and cd around it. On a side note, if you try to cd to a server folder in Windows you will see the error "CMD does not support UNC paths as current directories", in which case use the above command to stop this error.

To remove this server directory just use the command popd, which will remove the last network drive you mapped.

popd

Sometimes you might want to force all files to be copied, regardless of their creation time, in which case you can just run the xcopy command without the /D flag.

xcopy c:\mysourcedirectory c:\mydestinationdirectory /D /I /E /F /Y /H /R

One word of warning when using xcopy. It isn't as sophisticated as rsync so if you rename or delete files from your source directory, they won't be changed in your destination directory. This caused me some problems recently when I changed some filename and locations and then used xcopy to copy a plugin from my localhost to a server. The autoloader I used loaded all of the files and produced fatal errors as it couldn't find some of the class files referenced in the old files.

If you need the flexibility that comes with rsync then you should probably use one of the synchronisation programs available, or even a source control mechanism.

Comments

Its very informative and also very technical. I think I have to ask pc experts with this one.

Permalink

Hi  philipnorton, I'm using Xcopy script for data backup, everything is good but my question is my data is appending day-by-day. I used /D command too but still some files are showing at destination after deleting at source. so please help me for this. My script is 

> XCOPY "M:\FOLDER1" Z:\FOLDER2 /D /I /E /F /Y /H /R

Thanks in advance.

Permalink

I think you probably need to add a deletion step in before you do the xcopy. I found that if I deleted any files on the source side they would still be in the destination side unless I specifically got rid of them.

My guess is that you are looking for a more sophisticated sync tool than xcopy ;)

Name
Philip Norton
Permalink
xcopy is pretty good though we run into a few limitations such as copying open and locked files. we eneded up using a utility called gscopypro which works just like robocopy however it can also copy open files. Works great. It is by gurusquad http://www.gurusquad.com/GSCOPYPRO
Permalink
I was searching for something else ... but I read of "pushd" command, which is totaly new for me! Great: I will save my ass in many cases! Thank you! :)
Permalink
Windows command line has mainly robocopy, shadowcopy or this xcopy. But windows is a purely made for GUI experience for users who dont know command line then why to introduce these and give pain to them. I had tried using xcopy and I dont think I will ever be able to use it, with so many commands to remember, I just could not follow. After searching a lot on the internet, reading blogs I found GS Richcopy 360 a really good and effective software with an easy to use GUI. Its better than these command line utilities. It has helped me a lot with my backup and restore. I have been using it for quite a while and its a lot better than other software, although its paid but its worth every penny.
Permalink
"The easiest thing to do is make a batch file called xcopy_sync, put it in the windows folder and use that instead. XCOPY_SYNC.BAT REM Sync files xcopy /d /e /s /y %1 %2 Now you can just say ""xcopy_sync source dest"" and it will remember all the parameters for you and is also advisable since you can never make a mistake with the parameters. When typing from the command line any error in the flags could do horrible things to your sync target folder for example..currently i am using gs richcopy 360 enterprise for file sync, it's better than xcopy in my opinion....it has features like copying long file path names, copying NTFS file permissions and is not too costly.. "
Permalink

I used xcopy right up until my third heart attack.

Then I switched to GS RichCopy 360 Enterprise and now the tomatoes in my garden grow bigger than ever!

 

Permalink

After trying out several GUI-based tools the command line was simpler and faster. Thanks !

Permalink

write the sync once and put it in a .bat file then run the BAT file through a Windows Schedular no need to remember anything or interact with it again unless you want to manually run it or stop it 

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
3 + 10 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.