Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. $ebookLibraryPath = '.\OneDrive\Documents\Ebook Library\'
  2. Function Get-Empties {
  3. PARAM($targetPath)
  4. Return Get-ChildItem -Path $targetPath -Recurse | Where { $_.PsIsContainer -eq $true } | Where { $_.GetDirectories().Count -eq 0 -and ($_.GetFiles().Count -eq 0) -or ($_.GetFiles().Count -eq 1 -and $_.GetFiles().Name -match "(?i)(.*?)\.opf$")}
  5. }
  6. $failed = @{}
  7. $toEmpty = Get-Empties $ebookLibraryPath
  8.  
  9. Function Write-Log {
  10. Param ( [parameter(Mandatory=$true,
  11. ValueFromPipeline=$true)]
  12. [string]$logMessage,
  13. [PSDefaultValue(Help = "Will create a log file in the same directory as the script is run")]
  14. [string]$Logfile = ".\$(gc env:computername)-delete-dirs.log")
  15.  
  16. Add-Content $Logfile -value $($(Get-Date).ToString() + ": " + $logMessage)
  17. }
  18.  
  19. while($toEmpty.Count -gt 0) {
  20. try {
  21. foreach($dir in $toEmpty){Write-Log "Deleting $($dir.FullName) which has LastWriteTime $($dir.LastWriteTime.ToString())"; Remove-Item -LiteralPath $dir.PSPath -Force -Recurse -Confirm}
  22. }
  23. catch {
  24. $ErrorMessage = $_.Exception.Message
  25. $FailedItem = $_.Exception.ItemName
  26. Write-Error -Exception $_.Exception
  27. $failed.Add($failed.Count+1,$dir)
  28. }
  29. $toEmpty = Get-Empties $ebookLibraryPath
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement