Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ebookLibraryPath = '.\OneDrive\Documents\Ebook Library\'
- Function Get-Empties {
- PARAM($targetPath)
- 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$")}
- }
- $failed = @{}
- $toEmpty = Get-Empties $ebookLibraryPath
- Function Write-Log {
- Param ( [parameter(Mandatory=$true,
- ValueFromPipeline=$true)]
- [string]$logMessage,
- [PSDefaultValue(Help = "Will create a log file in the same directory as the script is run")]
- [string]$Logfile = ".\$(gc env:computername)-delete-dirs.log")
- Add-Content $Logfile -value $($(Get-Date).ToString() + ": " + $logMessage)
- }
- while($toEmpty.Count -gt 0) {
- try {
- foreach($dir in $toEmpty){Write-Log "Deleting $($dir.FullName) which has LastWriteTime $($dir.LastWriteTime.ToString())"; Remove-Item -LiteralPath $dir.PSPath -Force -Recurse -Confirm}
- }
- catch {
- $ErrorMessage = $_.Exception.Message
- $FailedItem = $_.Exception.ItemName
- Write-Error -Exception $_.Exception
- $failed.Add($failed.Count+1,$dir)
- }
- $toEmpty = Get-Empties $ebookLibraryPath
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement