Yesterday I wrote a PowerShell script that deletes files not accessed for more than 7 days.
Jeff Hicks correctly commented that such a script should be made into a function so that it can be called from the command line with a few parameters.
Basically: “script.ps1 D:\Company\DropBox 7”
First, I started to provide the users a nice little popup where they have to put in the parameters:
function DeleteOldFiles ($TargetPath, $DaysToKeep)
{
foreach ($i in Get-ChildItem $TargetFolder -recurse)
{
if ($i.LastWriteTime -lt ($(Get-Date).AddDays(-$daystokeep)))
{
Remove-Item $File.FullName -force
}}}
Command line: DeleteOldFiles d:\Company\DropBox 7
But… that script deletes all files, not only those with a specific extension. No problem, that is easily put in there:
function DeleteOldFiles ($TargetPath, $DaysToKeep, $Extension)
{
foreach ($i in Get-ChildItem $TargetFolder –recurse –include $Extension)
{
if ($i.LastWriteTime -lt ($(Get-Date).AddDays(-$daystokeep)))
{
Remove-Item $File.FullName -force
}}}
Command line: DeleteOldFiles d:\Company\DropBox 7 .zip

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 