I would like to mirror a directory, but only delete files in the destination directory older than 7 days.
Situation:
- Directory A is mirrored to Directory B.
- A file from Directory A is deleted
I would like for that file to remain in Directory B for 7 days. After 7 days the file would be deleted if it still does not exist in Directory A.
Current Solution:
- Use Free File Sync to mirror Directory A to Directory B. Extra files in Directory B are moved (termed versioning within Free File Sync) to a temp directory
- Use a powershell script to update date modified to current date for all files in the temp directory
- Move contents of temp directory to a delete pending directory using robocopy
- Use Delage32 program to delete files and empty directories older (date modified) than 7 days in the delete pending directory.
There are two problems. One is the amount of steps required for this type of backup. More importantly, I have to use two temporary directories with excessive disk writes to essentially achieve what I'm after.
I could get away with using robocopy /mir option and delage32 if robocopy would update timestamps in the destination directory, even if no copies took place.. like a unix touch command incorporated into robocopy. Any suggestions or alternatives?
Best Answer
Here is a simple PowerShell script that will do what you are looking for. Change
FolderA
andFolderB
appropriately. Also, the-whatif
will just tell you what it will do without performing any actions. Once you verify it is what you are trying to do is correct, just remove the-whatif
.