Last active
July 20, 2021 08:43
-
-
Save rlipscombe/9903bea58476be717a58db9bf87fac54 to your computer and use it in GitHub Desktop.
Elixir snippet to find MP3 files with the same track number in their filenames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
re = ~r/^.*([0-9][0-9]) .*$/U | |
files = Path.wildcard("**/*.mp3") |> Enum.filter(&Regex.match?(re, &1)) | |
groups = files |> | |
Enum.group_by(fn f -> | |
d = Path.dirname(f) | |
f = Path.basename(f) | |
[_, n] = Regex.run(re, f) | |
{d, n} | |
end) | |
dupes = groups |> Enum.filter(fn {_, fs} -> | |
length(fs) > 1 | |
end) |> | |
Enum.reduce([], fn {_, fs}, acc -> | |
acc ++ fs | |
end) | |
dupes |> Enum.each(&File.rm/1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Warning: it deletes all the files with duplicate names (it doesn't keep the "correct" one), no prompting. I assume that you've got a pristine directory of .flac files somewhere else that you can recreate the correct MP3 files from.