Syncing Files Using The Windows Command Line

Wednesday, November 10, 2010 - 11:26

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:

1
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:

1
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.

1
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.

1
popd

Sometiems 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.

1
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 flexability that comes with rsync then you should probably use one of the synchronisation programs available, or even a source control mechanism.

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

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

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.

philipnorton42's picture
Submitted by philipnorton42 on Tue, 05/01/2012 - 14:29

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 ;)

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

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! :)

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.

Add new comment