Skip to content

Instantly share code, notes, and snippets.

@rlipscombe
Last active July 20, 2021 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlipscombe/9903bea58476be717a58db9bf87fac54 to your computer and use it in GitHub Desktop.
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
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)
@rlipscombe
Copy link
Author

rlipscombe commented Jul 20, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment