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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior 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
oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
Wow, I really didn't thought about robocopy :) Now mr oBdA you just gave me another idea, great thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.