Powershell copy files 92 days ago to another folder.

Dear expert

Please read the script below... I want all files that created 92 days ago copy them to c:\web folder... Now this code copies 2 times. I don't know where seems the problem?

Thanks

$date= Get-Date -format "yyyyMMdd"
$limit = (Get-Date).AddDays(-92)
$sourcepath = 'c:\temp'
$oldpath='c:\temp'
$newpath='c:\web'
$filer=Get-ChildItem -Path $sourcepath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -gt $limit}
$filer|foreach-object {
          $olddir=$_.DirectoryName
          $newdir=$olddir -ireplace [regex]::Escape($oldpath), $newpath
          write-host "copy $olddir\$($_.name) to $newdir\$($_.name)"
		  if (!($newdir -eq "")) {
			new-item -itemtype File -path "$newdir\$($_.name)" -force
			copy-item -literalpath "$olddir\$($_.name)" -destination "$newdir\$($_.name)" -Recurse 
		  }
		  $newdir=""
}

Open in new window

LVL 1
WeTiAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
You can't just remove the "new-item -itemtype File ..." - that's the command that will create the target directory if it doesn't exist yet.
In other words: the script should work as designed.
But what it tries to do is to copy all files newer (" $_.CreationTime -gt $limit") than 92 days - is that really your intention?

That said: "CreationTime" is treacherous - it is the time the file is created on the drive, not the time some user created it. If you copy, for example, a file with a LastWriteTime of 3 months ago to another folder using Explorer or Copy-Item, the copy's CreationTime will be the time you copied it.
Instead of using this script, you can use a single robocopy command (based on LastWriteTime, not CreationTime).
The following examples are in test mode and will only list the files they would normally copy. Remove the "/L" to run it for real.
To copy only files older than n days, use /MINAGE:n:
robocopy.exe C:\Temp C:\Web *.* /s /r:0 /MINAGE:92 /L

Open in new window

To copy only files newer than n days, use /MAXAGE:n:
robocopy.exe C:\Temp C:\Web *.* /s /r:0 /MAXAGE:92 /L

Open in new window

Fun facts:
* robocopy will by default skip files that already exist in the destination (same size, same time).
* Unlike Explorer, robocopy will by default copy the time stamps, including CreationTime.
0
 
ste5anConnect With a Mentor Senior DeveloperCommented:
It does not copy it twice. But you create a blank one first (new-item), then you copy the file (copy-item). Thus two actions.
0
 
WeTiAuthor Commented:
Isn't it just to remove the: new-item -itemtype File -path "$newdir\$($_.name)" -force ?
 
$date= Get-Date -format "yyyyMMdd"
$limit = (Get-Date).AddDays(-92)
$sourcepath = 'c:\temp'
$oldpath='c:\temp'
$newpath='c:\web'
$filer=Get-ChildItem -Path $sourcepath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -gt $limit}
$filer|foreach-object {
          $olddir=$_.DirectoryName
          $newdir=$olddir -ireplace [regex]::Escape($oldpath), $newpath
          write-host "copy $olddir\$($_.name) to $newdir\$($_.name)"
		  if (!($newdir -eq "")) {
			new-item -itemtype File -path "$newdir\$($_.name)" -force
			copy-item -literalpath "$olddir\$($_.name)" -destination "$newdir\$($_.name)" -Recurse 
		  }
		  $newdir=""
}

Open in new window

0
 
WeTiAuthor Commented:
If it's not correct then lets rewrite the whole thing, I would like to copy from a network path to another from 92 days ago using syntax below:
$limit = (Get-Date).AddDays(-92)
{ !$_.PSIsContainer -and $_.CreationTime -gt $limit}
0
 
WeTiAuthor Commented:
Wow, I really didn't thought about robocopy :) Now mr oBdA you just gave me another idea, great thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.