jenny m
jenny m

Reputation: 11

Copy files in a date range while preserving folder structure

I need to copy from a huge archive a subset of the files that were modified in a given date range. I also need to preserve the directory structure.

Example of powershell code that may work:

#Delete dest
Remove-Item $destPath\* -Recurse -Force
#copying file from srcPath to destPath
Get-ChildItem -Path $srcPath | Copy-Item -Destination $destPath -Recurse -Container
#deleting files according to date
Get-ChildItem -Path $destPath -Recurse | Where-Object { $_.lastWriteTime -lt ($Curr_date).adddays($Max_days) -or $_.LastWriteTime -gt ($Curr_date).adddays($Min_days) } | Remove-Item -Force
Write-Host "Done"

The problem with the above is that I am needlessly copying files. Imagine that I have a huge archive of files, it will be prohibitively costly to copy them all.

Thanks in advance for any help!

Upvotes: 0

Views: 1115

Answers (1)

jenny m
jenny m

Reputation: 11

I am using this command in the end

Robocopy $srcPath $destPath *.* /MAXAGE:$Max_days /MINAGE:$Min_days /S

Upvotes: 1

Related Questions