我想镜像一个目录,但是只能删除大于7天的目标目录中的文件。
情况:
- Directory A is mirrored to Directory B. - A file from Directory A is deleted
我希望这个文件保留在目录B中7天。 7天之后,如果文件A中仍然不存在,文件将被删除。
当前解决scheme
- 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.
有两个问题。 一个是这种备份所需的步骤数量。 更重要的是,我必须使用两个具有过多磁盘写入的临时目录来基本实现我所追求的目标。
如果robocopy会更新目标目录中的时间戳,即使没有复制发生,我也可以使用robocopy / mir选项和delage32 ..就像一个并入到robocopy中的unix touch命令。 任何build议或替代?
这是一个简单的PowerShell脚本,可以完成你正在寻找的任务。 相应地更改FolderA和FolderB 。 另外,这个-whatif只会告诉你如果不采取任何行动,它会做什么。 一旦你确认了你正在做的是正确的,只要删除-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}