Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Remove_FilesCreatedBeforeDate{
- $LogPaths= get-content -path D:\ClearLogs\logpaths.txt
- $ValidPath = Test-Path $LogPaths -IsValid
- $Date=(Get-Date).AddDays(-15)
- If ($ValidPath -eq $True) {
- Foreach ($Path in $LogPaths) {
- if ($Path -ne $NULL) {
- Write-Host ">>> >> > $Path is OK and Cleanup is now running < << <<<" -ForegroundColor "yellow"
- $Folders = Get-ChildItem -Directory -Path $path -Recurse
- Write-Host ">>> >> > $($Folders.count) total folders < << <<<" -ForegroundColor "green"
- Foreach ($f in $Folders) {
- $Files = Get-ChildItem -File -Path $f.fullname -Recurse | Where-Object { $_.LastWriteTime -lt $Date }
- Write-Host ">>> Check folder $($f.fullname) <<<" -ForegroundColor "darkcyan"
- Write-Host ">>> $($Files.count) files older than $Date <<<" -ForegroundColor "cyan"
- if ($Files -ne $NULL) {
- #Remove-Item -Path $Files.fullname -Recurse -whatif ########----> THIS IS A TEST
- Write-Host ">>> $($Files.count) deleted files older than $Date <<<" -ForegroundColor "green"
- Remove-Item -Path $Files.fullname -recurse ########----> THIS IS NOT A TEST
- } else {
- Write-Host ">>> No files to delete <<<" -ForegroundColor "darkgreen"
- }
- $Empty = $f | Where-Object {(Get-ChildItem $_.FullName -File -Recurse -Force).Count -eq 0}
- Write-Host ">>> $($Empty.count) empty folders <<<" -ForegroundColor "magenta"
- if ($Empty) {
- #Remove-Item -LiteralPath $f.FullName -Exclude "LOG_PATH" -recurse -force -whatif ########----> THIS IS A TEST
- Write-Host ">>> $($Empty.count) empty folders deleted <<<" -ForegroundColor "green"
- Remove-Item -LiteralPath $f.FullName -Exclude "LOG_PATH" -recurse -force ########----> THIS IS NOT A TEST
- }
- }
- }
- }
- }
- Else {Write-Host "Path is not a ValidPath"}
- }
- Remove_FilesCreatedBeforeDate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement