0

I compare files inside a folder. In this folder some files are existing in two file formats (filename1.jpg, filename1.bmp, ...) and some files are only existing in one format. I try to find all files which are only existing in .bmp format and delete them.

The Code I got so far is:

$jpg = Get-ChildItem "C:\..\" -File -Filter *.jpg | ForEach-Object -Process {[System.IO.Path]::GetFileNameWithoutExtension($_)}
$bmp = Get-Childitem "C:\..\" -File -Filter *.bmp | ForEach-Object -Process {[System.IO.Path]::GetFileNameWithoutExtension($_)}

Compare-Object $jpg $bmp | where {$_.SideIndicator -eq  "=>"} 

This lists me the files I am looking for but I have trouble deleting them. I tried some things like:

Compare-Object $jpg $bmp | where {$_.SideIndicator -eq  "=>"} | ForEach-Object {
    Remove-Item  "C:\..\($_.FullName)"
    }

but without any success. Does anyone have a hint how I could solve this?

axl258
  • 3
  • 1

1 Answers1

0

In your foreach your variable is not a file, it is the result from the compare.

Try this:

$a = Get-ChildItem "D:\a" -File -Filter *.jpg
$b = Get-Childitem "D:\b" -File -Filter *.bmp
Compare-Object $a.BaseName $b.BaseName | where {$_.SideIndicator -eq  "=>"}  | foreach {
    $name = $_.InputObject
    $File = $b.Where({$_.BaseName -eq $name})

    if ($File.Count -gt 1) {
        throw "Conflict, more than one file has the name $name"
    } else {
        Remove-Item -Path $File.FullName
    }
}
guiwhatsthat
  • 2,349
  • 1
  • 12
  • 24