POWERSHELL 50
DeleteIfLinesv2 Guest on 28th March 2019 03:46:08 PM
  1. #########################
  2. ##### Configuration
  3. #########################
  4. $Target_Directory = "C:\Users\DEDE\Desktop\Test\AllFiles"
  5. $Deleted_Folder = "C:\Users\DEDE\Desktop\Test\DeletedFiles"
  6. $Max_Lines = 9
  7. $Delete = $false
  8.  
  9.  
  10. ###############################
  11. ###### Check if admin
  12. ###############################
  13. $Is_Admin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
  14. if (-Not ($Is_Admin)) {
  15.     Write-Host 'You have to run this script as Administrator.'
  16.     write-host "Press any key to continue..."
  17.     [void][System.Console]::ReadKey($true)
  18.     Exit
  19. }
  20.  
  21.  
  22. #########################
  23. ##### Main
  24. #########################
  25. Clear-Host
  26. Write-Host "=== Start" -BackgroundColor Black
  27. $Total_Files = 0
  28. $Deleted_Files = 0
  29. $Skipped_Files = 0
  30. Get-ChildItem $Target_Directory -Recurse -Include *.* |
  31. Foreach-Object {
  32.     $Total_Files++
  33.     $Current_Lines = Get-Content $_.FullName | Measure-Object -Line
  34.     $Current_Lines = $Current_Lines.Lines
  35.     # Check if lines are less than
  36.     if ($Current_Lines -lt $Max_Lines) {
  37.         $Deleted_Files++
  38.         if ($Delete) {
  39.           Write-Host "$($_.FullName) has $Current_Lines/$Max_Lines lines: deleting..." -BackgroundColor Red
  40.           Remove-Item -Path $_.FullName -Force
  41.         } else {
  42.           Write-Host "$($_.FullName) has $Current_Lines/$Max_Lines lines: moving..." -BackgroundColor Blue
  43.           $Temp_Destination = "$Deleted_Folder\$($_.Name)"
  44.           Move-Item -Path $_.FullName -Destination $Temp_Destination
  45.         }
  46.     } else {
  47.         Write-Host "$($_.FullName) has $Current_Lines/$Max_Lines lines: skipping..." -BackgroundColor Green
  48.         $Skipped_Files++
  49.     }
  50. }
  51. Write-Host "===== Results" -BackgroundColor Black
  52. If ($Delete) {
  53.   Write-Host "Total Files: $Total_Files | Deleted Files: " -NoNewLine -BackgroundColor Black
  54.   Write-Host "$Deleted_Files" -ForegroundColor Red -NoNewline -BackgroundColor Black
  55. } else {
  56.   Write-Host "Total Files: $Total_Files | Moved Files: " -NoNewLine -BackgroundColor Black
  57.   Write-Host "$Deleted_Files" -ForegroundColor Blue -NoNewline -BackgroundColor Black
  58. }
  59.  
  60. Write-Host " | Skipped Files: " -NoNewLine -BackgroundColor Black
  61. Write-Host "$Skipped_Files " -ForegroundColor Green -BackgroundColor Black

RSO cPaste е място за публикуване на код или текст за по-лесно отстраняване на грешки.

Влез или се Регистрай за да редактираш, изтриваш или преглеждаш хронология на твоето публикувано съдържание

Необработен текст

Влез или се Регистрирай за да редактираш или задържиш това съдържание.