I would like to mirror a directory, but only delete files in the destination directory older than 7 days.


- 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?

1 Answer 1


Here is a simple PowerShell script that will do what you are looking for. Change FolderA and FolderB 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.

#This sets $FolderA to the directory you want to copy from    
$FolderA = "v:\FolderA"
#This sets $FolderB to the directory you want to copy to
$FolderB = "v:\FolderB" 
#This does the copy (Note the -whatif to make sure this is what you want)
Copy-Item -Path "$FolderA\*" -Destination $FolderB -WhatIf
#This does a compare of Directory A and B, and removes all files that only exist in Directory B that haven't been access for 7 days. (Again, notices the -whatif at the end)
Compare-Object (Get-ChildItem $FolderA) (Get-ChildItem $FolderB) ` #The [`] tells PowerShell the command will continue on the next line
    | where {$_.SideIndicator -eq "=>"} `
    | where {$_.InputObject.LastWriteTime -le (Get-Date).Adddays(-7)} `
    | Foreach { Remove-Item -Path $_.InputObject.FullName -WhatIf}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .