bana201
bana201

Reputation: 15

remove duplicate files not in folder in powershell

I want to write a script that will delete duplicate files and keep one copy where any of that file's parent directory is called "Final"

in other words, do not delete if none of the same files(files with the same hash)' parent dir is called "Final"

UPDATE

this is what I have so for, not sure how to go from here

ls *.* -recurse | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group } | select {$_.Path}

I need to filter out $_.Path that contain the directory "Final" tried where { $_.Directory -contains "Final"} but didn't work

Upvotes: 1

Views: 373

Answers (1)

Mark Harwood
Mark Harwood

Reputation: 2415

Looks like you've just copied the most upvoted response to this question without thinking or trying to solve the issue.

So far you are just looping through all the files and deleting anything that has the same hash twice.

You would need to add another check for the full path containing "Final". Let me know what you think of the below code. I haven't fully tested it yet, however...

Get-ChildItem -Path c:\Path\Here |
    Where-Object { $_.FullName -NotLike "*Final"} |
    Get-FileHash |
    Group-Object -Property Hash |
    Where-Object { $_.Count -gt 1} |
    Foreach-Object { $_.Group | Select-Object -Skip 1 } |
    Remove-Item

Upvotes: 1

Related Questions