Reputation: 477
I have log files being created in one of my log directory through a powershell. I want to incorporate in the powershell itself a piece of code that will delete the log files from the directory that are logged a couple of days before.
My requirement is my log files should be present only for two days back from current day and rest should be deleted automatically as the powershell runs.
my log files names are as follows which has a time stamp at the end:
abc_log.2013_06_25_17_39_21.log
Could anyone please help me in sorting this out with their valuable piece of code?
Upvotes: 0
Views: 665
Reputation: 132
Not powershell tool, but for the practical purposes you may use DELOLDER utility, which can erase files older then specifed age. You may determine file for erasing only some extensions also, and whether it removing with containig old files folder or old files only.
Upvotes: 0
Reputation: 126702
The basic idea is to compare the file's creation date to a specific date time.
$old = (Get-Date).AddDays(-2)
Get-ChildItem $path -Filter *.log |
Where-Object {!$_.PSIsContainer -and $old -ge $_.CreationTime} |
Remove-Item
Upvotes: 1