Move Files Older Than X Days

Hello,

I have the following code that performs the following:   I would like to modify the code to move files older than xdays to a network share.  Can you help please?   I have the following answers from a previous case to delete files older than x days here: http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28601783.html

Script Begin
[CmdletBinding(SupportsShouldProcess=$True)]
Param(
	[string]$Path = "C:\Temp\DeleteMe",
	[string]$Days = 3
)
If (-Not (Test-Path -Path $Path)) {
	"Path '$Path' not found!" | Write-Error
	Exit 1
}
Get-ChildItem $Path -Recurse |
	? {-Not $_.PsIsContainer -And ($_.LastWriteTime -lt (Get-Date).AddDays(-$Days))} |
	Remove-Item

End

Open in new window

LVL 9
stressedout2004Asked:
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.

becraigCommented:
Param(
      [string]$Path = "C:\Temp\DeleteMe",
      [string]$Days = 3
)
If (!(Test-Path -Path $Path)) {
      "Path '$Path' not found!" 
	exit;
}
else 
{
gci $Path -recurse | ? {!($_.psiscontainer) -and $_.LastWriteTime -lt (Get-date).AddDays(-$days)} | Remove-Item
}

Open in new window

0
Bill PrewIT / Software Engineering ConsultantCommented:
How about this?

[CmdletBinding(SupportsShouldProcess=$True)]
Param(
	[string]$Path = "C:\Temp\DeleteMe",
	[string]$Dest = "\\server\share\folder",
	[string]$Days = 3
)
If (-Not (Test-Path -Path $Path)) {
	"Path '$Path' not found!" | Write-Error
	Exit 1
}
Get-ChildItem $Path -Recurse |
	? {-Not $_.PsIsContainer -And ($_.LastWriteTime -lt (Get-Date).AddDays(-$Days))} |
	Move-Item -dest $Dest

End

Open in new window

~bp
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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Bill's mod should work (becraig didn't really change anything), but it is better to have the date calculation done once only:
[CmdletBinding(SupportsShouldProcess=$True)]
Param(
  [string]$Path = "C:\Temp\DeleteMe",
  [string]$Dest = "\\server\share\folder",
  [string]$Days = 3
)
If (-Not (Test-Path -Path $Path)) {
  "Path '$Path' not found!" | Write-Error
  Exit 1
}
$thePast = (Get-Date).AddDays(-$Days)
Get-ChildItem $Path -Recurse |
  ? { !$_.PsIsContainer -and $_.LastWriteTime -lt $thePast } |
  Move-Item -dest $Dest

Open in new window

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.