如何使用批处理命令将文件从文件夹树复制到单个文件夹而只复制最新文件?
这些副本必须每隔一小时发生一次,避免覆盖而不是仅复制最新文件。
当前命令:
for /R "D:\Logshipping\NDWAnalyzer\" %%f in (*.*) do copy "%%f" "D:\LogShipping1\NDWAnalyzer\"
上面的命令每次复制文件夹中的整个文件而不是我只需要复制最新文件 - 目标文件夹中不存在的文件。
答案 0 :(得分:0)
这是一个简单的任务,使用带有参数/M
的{{3}},这样只会复制设置了存档属性的文件,并在复制后清除此属性。将为新文件或已修改的文件自动设置存档属性。
@echo off
rem Check before copying for deletion of files in destination directory
rem which were copied already before and not updated in the meantime, i.e.
rem no archive attribute set anymore on file, but file is also not existing
rem anymore in destination directory. The archive attribute is set temporarily
rem on those files before running xcopy. This helps also in case of renaming
rem a file in one of the source directories which was copied already before
rem with previous name to destination directory as the rename operation does
rem not set archive attribute.
for /R "D:\Logshipping\NDWAnalyzer" %%F in (*) do (
if not exist "D:\LogShipping1\NDWAnalyzer\%%~nxF" %SystemRoot%\System32\attrib.exe +a "%%~F"
)
rem Copy from all directories in D:\Logshipping\NDWAnalyzer all files with
rem archive attribute set (= modified or added since last run) to directory
rem D:\LogShipping1\NDWAnalyzer with clearing the archive attribute after
rem copy and with copying also all attributes including read-only attribute.
for /R "D:\Logshipping\NDWAnalyzer" %%D in (.) do (
%SystemRoot%\System32\xcopy.exe "%%~fD\*" "D:\LogShipping1\NDWAnalyzer\" /C /H /I /K /M /Q /R /Y >nul
)
如果您想使用第一个+第二个循环或仅使用第二个循环,则由您决定。
删除第一个循环后,可以删除源目录中仍然存在的目标目录中的文件,或者重命名之前已复制到目标目录的源目录中的文件,而不再复制。
有关使用过的命令的更多详细信息,请打开命令提示符窗口并运行
attrib /?
for /?
if /?
xcopy /?
为每个命令输出一个帮助,应该读取该命令以理解上面的两个循环。