I want to use a batch file to synchronize 2 folders on Windows, and I'm using the xcopy command like this
我想在Windows上使用批处理文件来同步两个文件夹,我使用的xcopy命令如下
xcopy /s /d <Original Folder> <Destination Folder>
It's working perfectly because I just want to overwrite only the modified ones, but I have 1 tiny problem, when I delete a file from the original folder, that file remains in the destination folder when i run the batch file, breaking the synching I want to achieve... I think this can't be done with xcopy, but I don't know which command I should use in case I delete a file in the original folder.
它工作得很好,因为我只想覆盖修改过的文件,但我有一个小问题,当我从原始文件夹中删除文件时,当我运行批处理文件时,该文件仍保留在目标文件夹中,破坏了我想要实现的同步。。。我认为xcopy无法做到这一点,但我不知道在删除原始文件夹中的文件时应该使用哪个命令。
EDIT: This programs is for people who don't understant a lot of PC's so i don't want to install anything in their pc's. Just want a plain batch file to keep the 2 folders synchronized
编辑:这个程序是为那些不了解很多电脑的人准备的,所以我不想在他们的电脑上安装任何东西。只需要一个普通的批处理文件来保持两个文件夹的同步
SOLUTION: Thanks to Kuba Wyrostek I got to this command
解决方案:多亏了Kuba Wyrostek,我得到了这个命令
robocopy <Original Folder> <Destination Folder> /e /purge
robocopy<原始文件夹><目标文件夹>/e/清除
Thank you all for your time
谢谢大家抽出时间
更多回答
xcopy
has no such option. Your alternatives are:
xcopy没有这样的选择。您的备选方案是:
rsync - http://rsync.samba.org
rsync-http://rsync.samba.org
robocopy - http://technet.microsoft.com/en-us/library/cc733145.aspx
自动复制-http://technet.microsoft.com/en-us/library/cc733145.aspx
To refine your answer, you can simply do:
robocopy <Original Folder> <Destination Folder> /mir
要完善您的答案,您只需执行以下操作:robocopy<原始文件夹><目标文件夹>/mir
/mir
mirrors a directory tree (equivalent to /e
plus /purge
).
/mir镜像一个目录树(相当于/e-plus/purge)。
I know this is old, but I have a question. Can this not be done with the correct xcopy commands.
我知道这是旧的,但我有一个问题。这不能用正确的xcopy命令来完成吗。
Folder 1: c:\workingFolder\"contents here"
文件夹1:c:\workingFolder\“此处的内容”
Folder 2: d:\workingFolderSync\"contents here"
文件夹2:d:\workingFolderSync\“此处的内容”
Batch file below
下面的批处理文件
xcopy "c:\workingFolder\*.*" "d:\workingFolderSync\" /i /s /d /y
xcopy "d:\workingFolderSync\*.*" "c:\workingFolder\" /i /s /d /y
pause
( I usually remove the pause after i test and know the batch works. )
But this will only copy contents from the folder to the other if its newer.
Then it looks in the destination folder and copies contents from destination back to source if it is newer. This reproduces what syncing does, except for the in real time monitoring of modified files. Creating a proper task schedule can actually implement running this command at specific times to almost perfectly reproduce syncing. You can also run this before modification on a machine and after modification on a machine. Even doing the same thing on a second machine if your using a network drive to sync.
(我通常在测试并知道批处理有效后删除暂停。)但这只会将内容从文件夹复制到另一个文件夹(如果它是新的)。然后,它会查找目标文件夹,并将内容从目标复制回源文件夹(如果更新)。这复制了同步的作用,除了实时监视修改的文件。创建一个合适的任务计划实际上可以实现在特定时间运行此命令,从而几乎完美地再现同步。您也可以在机器上进行修改之前和修改之后运行此程序。如果你使用网络驱动器进行同步,即使在第二台机器上也要做同样的事情。
The XCOPY command for copying the updates from one folder to another is as follows:
用于将更新从一个文件夹复制到另一个文件夹的XCOPY命令如下所示:
C:\> XCOPY C:/SOURCE D:/DESTINATION /E /D /C /Y
The flags used in the above example have the following meanings:
上述示例中使用的标志具有以下含义:
/E
causes all folders and sub-folders to be copied
/D
causes a DATE comparison to be made, only copying items that are newer than the DESTINATION item. If the DESTINATION is older, or does not contain the file, then it will be copied.
/C
tells XCOPY to continue if it encounters an error - typically errors occur with read-only files, or files that have protected permissions
/Y
tells XCOPY to suppress prompting you for confirmation, which is necessary if you intend to create an automation script or scheduled task
Your method will have two main problems:
您的方法将有两个主要问题:
- Files deleted from one folder will be reinstated by the copy left in the other folder the next time you run your xcopy
- Renaming files can cause chaotic results.
I would suggest make use of robocopy, the copy resumes if there is a disconnection in the network. And it is faster than xcopy.
我建议使用自动复制,如果网络断开连接,复制会恢复。而且它比xcopy快。
Save the below lines in a bat or cmd file so that you can schedule its run
将以下行保存在bat或cmd文件中,以便安排其运行
set source=<Original Folder>
set target=<Destination Folder>
robocopy /mir %source% %target%
更多回答
This does not answer the original question.
这并不能回答最初的问题。
@geisterfurz007 Are you sure this is not the original? That article doesn't have a date, but its oldest disqus comment is 10 months after this post. If you believe a post is plagiarized, please raise a moderator flag.
@geisturz007你确定这不是原作吗?这篇文章没有日期,但它最古老的disqus评论是在这篇文章发布10个月后。如果你认为一篇文章是抄袭的,请举起主持人的旗帜。
@RyanBemrose Argh what have I done >.> Nifty little detail and an idea I did not even think about. Took away my comment and downvote. Thanks for the note and hint for potential future cases :)
@RyanBemrose Argh我做了什么>.>微不足道的细节和一个我甚至都没想过的想法。取消了我的评论并投了反对票。感谢您的留言和对未来潜在案例的提示:)
How is this an answer?
这是怎么一个答案?
我是一名优秀的程序员,十分优秀!