copy files created or modified today with robocopy

喜你入骨 提交于 2019-12-11 16:49:37

问题


I'm trying to create a batch file in Win7 that will copy any files that have been created or modified today and copy them to a destination with a similar directory structure. This is what I have so far:

set today="20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MINAGE:%today%

I know that /e copies empty directories and /xf excludes all files, but I'm not sure if that helps me. The code above seems to copy all files regardless of date, so I'm a little lost here.


回答1:


Assigning quotes to your variables is not a best practice and will cause problems with some commands if you try to quote the variable later on. Regardless that was not your problem. Your problem is you need to use the /MAXAGE option. Reading the help file you should see this:

/MAXAGE:n : MAXimum file AGE - exclude files older than n days/date.`

So your code should be:

set "today=20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MAXAGE:%today%

Going to assume you thought the options were for INCLUDE.




回答2:


robocopy's /MINAGE//MAXAGE options regard the full date and time, so specifying something like /MAXAGE:1 filters for files that have been modified within the last 24 hours.

If you want to process files which have been modified today only, hence regarding the date but not the time, you could use forfiles and its '/D' option, like this:

set "DEST=D:\backup\temp"
forfiles /P "C:\temp" /D +0 /C "cmd /C if @isdir==FALSE for %%Z in (@relpath) do @(2> nul md 0x22%DEST%\%%~Z\..0x22 & copy @relpath 0x22%DEST%\%%~Z0x22)"


来源:https://stackoverflow.com/questions/51459071/copy-files-created-or-modified-today-with-robocopy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!