本文介绍了XCOPY命令日期选项,用于复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can someone tell me if there is any way if we don't want to copy the files after the specified date.E.g. if i specify the date 10-MAY-2017 & 11-MAY-2017 and folder has files for 10 & 11 May 2017. So if want only the 10-MAY-2017 files to copied. is there is any way ?

推荐答案

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

For the task at hand, robocopy is the easiest way to do it:

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

尽管有开关/MINAGE的名称,但不是创建的名称,而是最后的修改日期.

Despite the name of switch /MINAGE, not the creation but the last modification date is regarded.

请注意,我指定了第二天的日期,因为给定日期的文件已被排除.

Note that I specified the date of the next day, because files of the given date are already excluded.

要仅复制2017年5月10日之后的文件,命令行必须如下所示:

To copy only files from 10-May-2017, the command line has to be like this:

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

因为robocopy/MAXAGE选项仍然包含给定的日期,而不是/MINAGE.

because the /MAXAGE option of robocopy still includes the given date, in contrast to /MINAGE.

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

In case you insist on using xcopy, here is a script based on xcopy that does the following steps:

  • 创建一个不能复制的文件列表,因为它们在给定的日期或以后被修改;为此使用xcopy /L /F /D::/L表示列出但不复制,/F定义为输出完全解析的源路径和目标路径,并且/D:定义最后修改日期;
  • 使用findstr从所有可用文件(xcopy /L /F)的列表中过滤出以上所有文件;
  • 复制普通目录树(xcopy /T);
  • 浏览筛选的文件列表,并按copy分别复制每个文件;
  • create a list of files that must not be copied, because they are modified on the given date or later; for this xcopy /L /F /D: is used: /L means to list but not copy, /F defines to output fully resolved source and destination paths, and /D: lets define a last modification date;
  • filter out all the files above from a list of all available files (xcopy /L /F) using findstr;
  • copy the plain directory tree (xcopy /T);
  • walk through the filtered file list and copy every single file individually by 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

这篇关于XCOPY命令日期选项,用于复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:30