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.
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.
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 ;)
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!
After trying out several GUI-based tools the command line was simpler and faster. Thanks !