问题
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 ?
回答1:
For the task at hand, robocopy is the easiest way to do it:
robocopy D:\Source D:\Destination *.* /S /MINAGE:20170510
Despite the name of switch /MINAGE
, not the creation but the last modification date is regarded.
In case you insist on using xcopy
, here is a script based on xcopy
that does the following steps:
- 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
) usingfindstr
; - copy the plain directory tree (
xcopy /T
); - walk through the filtered file list and copy every single file individually by
copy
;
This is the code:
@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
回答2:
/D:mm-dd-yyyy Copy files changed on or after the specified date. If no date is given, copy only files whose source date/time is newer than the destination time.
Example: xcopy /D:mm-dd-yyy
来源:https://stackoverflow.com/questions/44235682/xcopy-command-date-option-for-copying-files