Hello,
Been a long time since I've graced this amazing forum with a question, so I thought I would....or actually I'm forced to becuase I'm not sure how to proceed. I know this is a simple answer, but I'm not sure.
My situation is as follows. I need a script to delete files older than x days in a specified path. I took a look at Ying Li's script on myitforum.com, link is here:
http://myitforum.com/cs2/blogs/yli628/archive/2007/10/02/powershell-script-to-delete-files-older-than-certain-days.aspx
I have modified it for my use and it is as follows:
Function GetOldFile
{
param($Dir = $args[0],
$Days = $args[1]
)
#echo $strComputer $Dir $Days
if (Test-Path $Dir)
{
$Now = Get-Date
# Notice the minus sign before $days
$CreateTime = $Now.AddDays(-$days)
# Get all of the items within our path, whose lastwrite time is
Get-ChildItem $Dir -recurse |Where {$_.CreationTime -le "$CreateTime"}|Remove-Item -recurse -force -whatif| out-file v:\TestFolders\CleanLog.txt -append
}
}
GetOldFile
As you can see it takes two arguments a path and a numeric integer which corresponds to the date I want to keep. However, I need to keep the folders and delete the files. The folder structure is required to be kept. Can anyone give me a start on what to look at? Thanks.
Cheers,
Ayth