Home > front end >  PowerShell keep two file types & delete others
PowerShell keep two file types & delete others

Time:09-26

I'm trying to cleanup my e-book folders. I want to keep .mobi & .epub, and delete others with the same name but different extension.

For example:

  1. 1984.mobi
  2. 1984.pdf
  3. 1984.rtf

The script would delete 1984.pdf & 1984.rft. BUT, if there is only non-epub/mobi files, leave them alone.

For example:

  1. 1984.mobi
  2. 1984.pdf
  3. 1984.rtf
  4. AnimalFarm.lit

The script would delete 1984.pdf & 1984.rft, leaving the .mobi & epub, & AnimalFarm.lit as there were no .epub & .mobi versions.

I found the script below at: https://community.spiceworks.com/topic/2146107-how-to-find-and-remove-duplicate-files-with-same-name-but-different-extension

# Extensions to keep
$ExtRetain = ".jpg"
$ExtRetain2 = ".jpeg"

# Path
$Directory = "C:\Path\To\Folder"

# Get Unique file BaseNames in Folder e.g. image1, image2, image3, image4
$FilesToKeep = Get-ChildItem -Path $Directory | ?{!$_.PSIsContainer} | Select BaseName -unique

# Delete duplicates with different extensions
ForEach ($File in $FilesToKeep) {
#Check for preferred FileType, if found delete any other files with same name, but a different type     if(Test-Path ($File.BaseName   $ExtRetain)){
    Get-ChildItem -Path $Directory | Where {$_.BaseName -eq $File.BaseName -and $_.Extension -ne $ExtRetain} | Remove-Item -Force
    Continue #Skips to next filename without processing next if statement
    }
#If the preferred FileType was not found check for 2nd choice
if(Test-Path ($File.BaseName   $ExtRetain2)){
    Get-ChildItem -Path $Directory | Where {$_.BaseName -eq $File.BaseName -and $_.Extension -ne $ExtRetain2} | Remove-Item -Force
    }

The problem is that script would delete jpeg if jpg were found. In my case it would delete mobi if epub were found.

I tried changing the delete statement:

-and $_.Extension -contains $ExtRetain,$ExtRetain2} 
-and $_.Extension -notcontains $ExtRetain,$ExtRetain} 

neither had the desired effect.

CodePudding user response:

If I understand the question as adjusted by comments you want to delete files of any format other than .epub & .mobi but only if one of those same 2 also exist. For example, if we encounter a file AnimalFarm.pdf only delete it if there's an .epub and/or .mobi version of the same book.

$ExtensionsToRetain = @(
    '.epub'
    '.mobi'
)

$Directory = 'C:\temp\BookCleanup'

$Files = Get-ChildItem $Directory -File 

$ExtIndex = 
$Files |
Where-Object{ $_.Extension -in $ExtensionsToRetain }

$Files |
Where-Object{ $_.Extension -notin $ExtensionsToRetain -and $_.BaseName -in $ExtIndex.BaseName } |
Remove-Item

I can probably make this more eloquent, but this works. It creates an array of files that have .epub & .mobi extensions. Then uses a Where{} clause to find the files that don't have one of those extensions and who's basename isn't in $ExtIndex then deletes them.

This example only checks if "at least" 1 of the preferred formats exists. As currently written it will not guarantee that both of them exist.

  • Related