仅批量复制修改过的文件

16

我有两个文件夹(比如说,源文件夹和工作文件夹),这两个文件夹中包含了数百个其他的文件/文件夹。

现在,源文件夹会定期更新(即某些文件/文件夹可能会更改或添加)。我正在尝试保持这两个文件夹同步。

因此,我需要一个批命令,只有在“源”文件夹中新建/修改的文件/文件夹才会更新到“工作”文件夹中。

2个回答

17

您不需要一个批处理文件。只需使用xcopy命令并添加一些参数即可。尝试以下命令:

xcopy <path to source> <path to destination> /D /E /C /Q /H /R /Y /K 



source  Specifies the file(s) to copy.
destination Specifies the location or name of new files.
/A  Copies only files with the archive attribute set, doesn't change the attribute.
/M  Copies only files with the archive attribute set, turns off the archive attribute.
/D:m-d-y    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.
/EXCLUDE:file1 [+file2][+file3]...  Specifies a list of files containing strings. When any of the strings match any part of the absolute path of the file to be copied, that file will be excluded from being copied. For example, specifying a string like \obj\ or .obj will exclude all files underneath the directory obj or all files with the .obj extension respectively.
/P  Prompts you before creating each destination file.
/S  Copies directories and subdirectories except empty ones.
/E  Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/V  Verifies each new file.
/W  Prompts you to press a key before copying.
/C  Continues copying even if errors occur.
/I  If destination does not exist and copying more than one file, assumes that destination must be a directory.
/Q  Does not display file names while copying.
/F  Displays full source and destination file names while copying.
/L  Displays files that would be copied.
/H  Copies hidden and system files also.
/R  Overwrites read-only files.
/T  Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes empty directories and subdirectories.
/U  Copies only files that already exist in destination.
/K  Copies attributes. Normal Xcopy will reset read-only attributes.
/N  Copies using the generated short names.
/O  Copies file ownership and ACL information.
/X  Copies file audit settings (implies /O).
/Y  Suppresses prompting to confirm you want to overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite an existing destination file.
/Z  Copies networked files in restartable mode.
/B  Copies the Symbolic Link itself versus the target of the link.
/J  Copies using unbuffered I/O. Recommended for very large files.

2
嗯...非常感谢...您能解释一下哪个具体参数可以确保只复制修改过的文件/文件夹吗? - copenndthagen
1
请查看xcopy中提供的链接:http://www.computerhope.com/xcopyhlp.htm - Lucian
我正在尝试使用xcopy命令将C:\Documents and Settings\myName\Desktop\trial\source复制到C:\Documents and Settings\myName\Desktop\trial\destination,参数为/D /E /C /Q /H /R /Y /K。 - copenndthagen
1
据我所知,Subversion将版本信息存储在一个名为.svn的文件夹中。请查看xcopy命令的/exclude参数,以排除该文件夹中的所有文件。 - Lucian
有没有办法使用这个来删除不在源文件夹中的文件?例如,如果我想将文件test1.txt和test2.txt复制到名为“备份”的文件夹中,该文件夹包含test1.txt、test2.txt和old_irrelevant_file.txt,如何让它删除old_irrelevant_file.txt? - Connor
显示剩余2条评论

13

/D:m-d-y 复制在指定日期或之后更改的文件。如果没有指定日期,则仅复制源时间比目标时间新的文件。

因此,只需要使用 /D 和 /Y 参数来回答原始问题:

xcopy <source> <destination> /D /Y

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接