I can’t figure out how to write script that backpus only files that were created or modified previous day. So if I start script on 25.07 at 15:30, it backups files between 24.07 00:00 and 25.07 00:00.
If it is possible preffered way is by using robocopy. I know for /maxage -1 switch but it works for files that are 1 day old counting form the time script was started (problem is because it includes files from current day also).
set source="C:\Folder1"
set destination="F:\Folder2"
robocopy %source% %destination% /z /MAXAGE: -1
Robocopy gives you two options for specifying dates (for maxage, minage, maxlad, minlad) – either relative (n<1900) or fixed dates (otherwise, treated as yyyymmdd). Full syntax here.
You want to include files created or accessed on given date so you have to use min/max lad (last access date) and fixed dates, so let’s specify your criteria using robocopy syntax:
1. Exclude files unused since yesterday: use /maxlad (today_date – 1 day)
2. Exclude files used today: use /minlad (today_date)
Put those together:
robocopy source_dir destination_dir file_spec /maxlad:%today_minus_1% /minlad:%today%todayandtoday_minus_1vars must be dates in yyyymmdd format (eg.20120710) – how to get them? Well, if you’re constrained to pure batch you would have to find scripts to do the math for you, there are some available (eg. here) or write it yourself.If you can use powershell it’s pretty simple:
for /f %y in ('powershell get-date ^(get-date^).adddays^(-1^) -uformat %Y%m%d') do set today_minus_1=%ygets you first, andfor /f %t in ('powershell get-date -uformat %Y%m%d') do set today=%tsecond variable.So to summarize: set your date vars and then run robocopy (using them in excludes). It’s worth to use
/Loption when checking if it works properly, as actual copy will re-set access timestamps on your files!All vars and commands given as if executed directly from cmd line. If used in batch you will need to add some
%(and probably wise to use setlocal)Note: version I use (XP010) does not allow to use negative numbers or space as in your example