Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Remove_FilesCreatedBeforeDate{
  2. $LogPaths= get-content -path D:\ClearLogs\logpaths.txt
  3. $ValidPath = Test-Path $LogPaths -IsValid
  4. $Date=(Get-Date).AddDays(-15)
  5. If ($ValidPath -eq $True) {
  6.     Foreach ($Path in $LogPaths) {
  7.      if ($Path -ne $NULL) {        
  8.         Write-Host ">>> >> > $Path is OK and Cleanup is now running < << <<<" -ForegroundColor "yellow"
  9.        
  10.         $Folders = Get-ChildItem -Directory -Path $path -Recurse      
  11.         Write-Host ">>> >> > $($Folders.count) total folders < << <<<" -ForegroundColor "green"
  12.  
  13.         Foreach ($f in $Folders) {
  14.          $Files = Get-ChildItem -File -Path $f.fullname -Recurse | Where-Object { $_.LastWriteTime -lt $Date }
  15.          Write-Host ">>> Check folder $($f.fullname) <<<" -ForegroundColor "darkcyan"    
  16.          Write-Host ">>> $($Files.count) files older than $Date <<<" -ForegroundColor "cyan"    
  17.  
  18.             if ($Files -ne $NULL) {
  19.                 #Remove-Item -Path $Files.fullname -Recurse -whatif ########----> THIS IS A TEST
  20.                 Write-Host ">>> $($Files.count) deleted files older than $Date <<<" -ForegroundColor "green"    
  21.                 Remove-Item -Path $Files.fullname -recurse ########----> THIS IS NOT A TEST
  22.             } else {
  23.                 Write-Host ">>> No files to delete <<<" -ForegroundColor "darkgreen"                
  24.             }
  25.  
  26.             $Empty = $f | Where-Object {(Get-ChildItem $_.FullName -File -Recurse -Force).Count -eq 0}
  27.             Write-Host ">>> $($Empty.count) empty folders <<<" -ForegroundColor "magenta"
  28.  
  29.             if ($Empty) {
  30.                 #Remove-Item -LiteralPath $f.FullName -Exclude "LOG_PATH" -recurse -force -whatif ########----> THIS IS A TEST
  31.                 Write-Host ">>> $($Empty.count) empty folders deleted <<<" -ForegroundColor "green"
  32.                 Remove-Item -LiteralPath $f.FullName -Exclude "LOG_PATH" -recurse -force ########----> THIS IS NOT A TEST
  33.             }
  34.  
  35.         }
  36.         }
  37.     }
  38. }
  39. Else {Write-Host "Path is not a ValidPath"}
  40. }
  41.     Remove_FilesCreatedBeforeDate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement