XCOPY命令用于复制文件的日期选项

时间:2017-05-29 06:13:16

标签: batch-file

如果我们不想在指定日期之后复制文件,有人可以告诉我是否有任何方法。 例如。如果我指定日期2017年5月10日& 2017年5月11日,文件夹包含10& 2017年5月11日。所以如果只想复制10-MAY-2017文件。有什么办法吗?

2 个答案:

答案 0 :(得分:0)

对于手头的任务,robocopy是最简单的方法:

robocopy D:\Source D:\Destination *.* /S /MINAGE:20170510

尽管名称为/MINAGE,但不是创建,而是考虑最后修改日期。

如果您坚持使用xcopy,则此处是基于xcopy的脚本,执行以下步骤:

  • 创建一个不得复制的文件列表,因为它们在给定日期或之后被修改;使用此xcopy /L /F /D:/L表示列出但不复制,/F定义输出完全解析的源和目标路径,/D:定义最后修改日期;
  • 使用xcopy /L /F过滤掉所有可用文件(findstr)列表中的所有文件;
  • 复制普通目录树(xcopy /T);
  • 浏览已过滤的文件列表,并按copy;
  • 分别复制每个文件

这是代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "SOURCE=D:\Source"      & rem // (source directory)
set "DESTIN=D:\Destination" & rem // (target directory)
set "PATTERN=*.*"           & rem // (file pattern)
set "COPYDATE=05-11-2017"   & rem /* (last modification date; only files are copied
                              rem     which are modified earlier; check format!) */
set "TEMPFILE=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (list of source files not to copy)
set "COPYLIST=%TEMP%\%~n0_%RANDOM%.lst" & rem // (full list of files to copy)

rem // List files modified on or after given date:
> "%TEMPFILE%" (
    for /F "tokens=1 delims=>" %%F in ('
        xcopy /L /I /F /D:%COPYDATE% "%SOURCE%\%PATTERN%" "%DESTIN%" ^| find ":"
    ') do (
        set "FILE=%%F"
        rem /* Double every `\` as `findstr` uses such as escape character;
        rem    then append ` -> ` which is used by `xcopy /F` as separator: */
        setlocal EnableDelayedExpansion
        (echo(!FILE:\=\\!^> )
        endlocal
    )
)
rem /* List files modified before given date
rem    (actually the temporary `%COPYLIST%` file is not really necessary,
rem     but it is quite convenient for understanding what is going on; instead
rem     the below `for /F` loop could parse the output of this command line): */
xcopy /L /I /F "%SOURCE%\%PATTERN%" "%DESTIN%" | find ":" ^
    | findstr /V /B /L /I /G:"%TEMPFILE%" > "%COPYLIST%"
rem // Prepare directory tree as `copy` (below) cannot create directories:
xcopy /I /T "%SOURCE%\%PATTERN%" "%DESTIN%" > nul
rem // Copy files from list:
for /F "usebackq tokens=1* delims=>" %%E in ("%COPYLIST%") do (
    set "LEFT=%%E" & set "RIGHT=%%F"
    setlocal EnableDelayedExpansion
    ECHO copy /Y "!LEFT:~,-2!" "!RIGHT:~1!"
    endlocal
)
rem // Clean up temporary files:
del "%TEMPFILE%" "%COPYLIST%"

endlocal
exit /B

答案 1 :(得分:0)

/ D:mm-dd-yyyy复制文件在指定日期或之后更改。如果没有给出日期,则仅复制源日期/时间早于目的地时间的文件。

示例:xcopy / D:mm-dd-yyy

相关问题