How can I copy files with timestamps between 2 times and preserve the directory structure

Posted by brushwood on Stack Overflow See other posts from Stack Overflow or by brushwood
Published on 2010-03-15T21:12:58Z Indexed on 2010/03/15 22:59 UTC
Read the original article Hit count: 381

Filed under:

I want to copy files that have timestamps from the time the script begins to run and a hour previous. So I am basically trying to emulate robocopy but with minage and maxage going down to the exact time rather than days. So far I have this in powershell:

$now = Get-Date
$previousHour = $now.AddHours(-1)
"Copying files from $previousHour to $now"

function DoCopy ($source, $destination)
{
 $fileList = gci $source -Recurse
 foreach ($file in $fileList)
 {
  if($file.LastWriteTime -lt $now -and $file.LastWriteTime -gt $previousHour)
  {
   #Do the copy
  }
 }
}

DoCopy "C:\test" "C:\test2"

but if I try to do the copy like that, it copies all the files directly into the destination folder rather than the subfolders.enter code here

© Stack Overflow or respective owner

Related posts about powershell