2
\$\begingroup\$

I have a drive with many thousands of sound effects. The files have been stored on different operating systems and NAS devices over time and there are now duplicates of many files, but the filenames contain different UTC timestamps. For example:

1-14 Crowded Bowling Alley (2016_06_28 02_14_41 UTC).aif

1-14 Crowded Bowling Alley (2016_02_18 05_56_59 UTC).aif

I would like to remove the duplicate files. There are also junk files left over from audio software and operating system indexing that I would like to remove. Using a copied subset of the data I have tested the following PowerShell script under PS version 5.1, it appears to give me what I'm after:

# Remove files left over from audio editors
Get-ChildItem -Name -Recurse -Filter "*.clip*" | Remove-Item
Get-ChildItem -Name -Recurse -Filter "*.ptf" | Remove-Item
Get-ChildItem -Name -Recurse -Filter "*.wfm" | Remove-Item
Get-ChildItem -Name -Recurse -Filter "*.repeaks" | Remove-Item

# Remove old indexing files
Get-ChildItem -Name -Recurse -Filter "*.DS_STORE" | Remove-Item
Get-ChildItem -Name -Recurse -Filter "._.*" | Remove-Item
Get-ChildItem -Name -Recurse -Filter "Thumbs*.db" | Remove-Item
Get-ChildItem -Name -Recurse -Filter "*.ini" | Remove-Item

# Remove UTC stamp from all files (will fail on duplicates)
Get-ChildItem -Recurse -Filter "*(?????????? ???????? UTC).*" | Rename-Item -newname { $_.basename.substring(0,$_.basename.length-26) + $_.Extension }

# Remove files that still have the UTC stamp (they were the duplicates)
Get-ChildItem -Name -Recurse -Filter "*(?????????? ???????? UTC).*" | Remove-Item

However, I would love to get a critique of the script and learn a few things. In particular it seems a little hacky to use an expected failed command as part of the workflow. It also seemed wise to get notes before I run it on the entire set of files.

\$\endgroup\$
1
\$\begingroup\$

While -Filter is usually the fastest way as it handled by the provider,
I'm not sure this is true compared to one -Include with an array which only recurses once instead of 8 times.

$Include = '*.clip*','*.ptf','*.wfm','*.repeaks','*.DS_STORE','._.*','Thumbs*.db','*.ini'

Get-ChildItem  -Recurse -Name -Include $Include | Remove-Item

As for the duplicates, your approach will only eliminate dupes in the same folder.
An alternative would be grouping the output with a calculated property which strips off the date in parentheses. And then iterating the groups and sorting by whatever means, keeping only the first/last - removing the others.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.