Recently I’ve found myself in a situation where I had to periodically delete files older than 7 days. I could have done it manually, but being busy, I often forgot to do it. The script I’m about to show you is a bit modified than the original, but, the principle is the same. Here I’ve added a posibilty to manually enter the directory name. In your script, you can define it, so it is being run automatically, without your intervention.
Here’s the code :
Function GetOldFile
{
param ($Dir = $(Read-host "Unesi putanju do direktorija")),
($Days = $(Read-Host "Brišem fileove starije od koliko dana?"))
$TargetFolder = $Dir
if (Test-Path $TargetFolder)
{
Write-host "DIREKTORIJ JE :" $TargetFolder -foregroundcolor "Red"
#Write-Host `a `a `a `a `a - odkomentirati ako zelimo da pc speaker vristi ![]()
Write-Host "UPS, krivi folder! Stisni 'Ctrl + C' za prekid - imas 5 sekundi za prekid" -foregroundcolor "Yellow"
Start-sleep -s 5
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -include *.log -recurse |Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{write-host "Brišem file $File" -foregroundcolor "Red"; Remove-Item $File | out-null}
}
Else
{Write-Host "Direktorij $TargetFolder ne postoji! Provjeri path!"}
}
GetOldFile
The questions in the script are in Croatian, but anyone with a little bit of programming experience will be able to change it, because the script is pretty straight forward. But, if you’re lazy as most of us are, here is the translated script
Working as a Windows server administrator many times I found myself in a situation where I have to check 50+ Mb log file for only one e-mail address or some other term. Ofcourse, many of you are going to think: “Yeah, like that’s a problem, just press CRTL+F and type in the term