1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

copy and pasted the wrong line, please see my update
@bana201 -Contains is for collections and not for strings :)
so the method you're using deals with objects? where vs where-object?
I have a phobia of shorthand code :-) They do the same thing. Just like ls and Get-ChildItem
I think you're method only detects duplicates in a folder named "Final". what i'm looking for is to keep the file in a folder name "Final" and delete duplicates elsewhere. so instead of Skip 1, Keep the the file in "Final"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.