Old File Cleaner

To go along with the purging of IIS log cleaner, this is a more generic recursive file cleaner using PowerShell for files based up on date specified.

#       --- Configuration Options ---
#Provide the name of the site
$mydir  = 'C:temp'
#Provide the number of days you wish to keep
$days = 4
#whatif toggle, verify only option
$whatif = $true

get-childitem $mydir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -WhatIf }
get-childitem $mydir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -ErrorAction SilentlyContinue} | Write-Host $_.fullname

In the case you need to run a similar function on older systems, here’s a more basic version suitable for a BAT file. FYI, there is no error checking option in this method.

FORFILES /P "C:temp" /S /D -7 /c "CMD /C del /F /Q @file"

Where 7 is the number of days you wish to keep the files. This also requires the FORFILES command to be available

, ,
January 31, 2011 at 7:51 am Comments (0)