Superpatterns Pat Patterson on the Cloud, Identity and Single Malt Scotch

15Sep/0812

Batch File to Delete Duplicate MP3s

Sharing this since it was pretty useful to me...

I noticed recently that I had a huge amount of duplicate files in my MP3 library. I think what had happened was that, on an iTunes upgrade, I'd rebuilt my iTunes index with iTunes set to 'Copy files to library'. The result was thousands of files with names like "01 I Can't Explain 1.mp3", where iTunes had copied "01 I Can't Explain.mp3" into the same directory, but had to rename the file.

So... What to do... Well, after seeing that the duplicate files were bit-for-bit identical to the original, but for the " 1" at the end of the filename, I realized I could write a simple batch file (my home PC runs Windows XP), that would find the duplicates, compare them with the originals to check that they were actually identical, then delete them:


@echo off
for /r "f:\my music\" %%I in (*.mp3) do (
if exist "%%~dpnI 1.mp3" (
echo Testing "%%~dpnI.mp3" against "%%~dpnI 1.mp3"
fc "%%~dpnI.mp3" "%%~dpnI 1.mp3" > nul
if not errorlevel 1 (
echo Match! Deleting "%%~dpnI 1.mp3"
del "%%~dpnI 1.mp3"
) else (
echo Found a non-match! "%%~dpnI.mp3" "%%~dpnI 1.mp3"
)
)
)

To be honest, it took me about an hour to figure out the right syntax for everything. I did try to neaten things up by assigning the "%%~dpnI 1.mp3" thing to a variable dupe, but gave up when I discovered that, in batch file land, variables are assigned when the line is read, not when it is executed (see this useful article for more explanation). To smarten up my batch file, not only did I have to use a strange !dupe! syntax ('delayed variable expansion'), but I had to run it with cmd /v:on /c deldupes. In the end, I decided not to bother.

I hate batch files.

Filed under: General Leave a comment
Comments (12) Trackbacks (0)
  1. I’m no scripter or programmer, but this could save my life. How do I run it?

  2. 1. Start/Run/notepad
    2. Copy everything from @echo off to the third closing parenthesis inclusive.
    3. Paste into notepad.
    4. Change the f:\my music\ to be wherever your MP3s are.
    5. File/Save – give it some name like c:\dedupe.bat – the .bat extension is important!
    6. Start/Run/cmd
    7. Type in ‘c:\dedupe.bat’ without the quotes

    The script should run. You can actually save it anywhere you like – I just used ‘c:\’ as it’s pretty much guaranteed to be there 🙂 The script reports on its progress, and only deletes files that are identical to the originals, so it should be pretty safe.

    Any problems – just let me know here in the comments.

  3. Nice Script! Now I just need to edit a few places to get it to delete duplicate .m4a, etc. Thanks for posting.

  4. i run your sample script but it could not delete any of my duplicates mp3, what seems to be the problem?

  5. Hi 203.131.163.204 – the script is very careful only to delete duplicates with exactly the same content as the original and the same name except for ‘ 1’ at the end. If your duplicates don’t meet these criteria, they won’t be deleted. Sorry.

  6. hi mr. pat patterson, i want to delete all my mp3 files tru script how can i do that?

  7. Delete ALL your mp3 files? Really?

    for /r "f:\my music\" %%I in (*.mp3) do del %%I

    would do it – you’ll need to change "f:\my music\" to match the location of your mp3 files, though.

  8. please give me sample script or a running script to delete all mp3 files… it would be a great help forme…

  9. i run the script that you gave, but it could not delete all my mp3 files,.. please give me complete script on how to delete all of my mp3 files,.. and how can i run it?

  10. hi, mr. pat patterson. sorry om not a programmer or a script developer that is why i cant easily catch your script and run it funtionally, please guide me on how can i delete mp3 files to all of my workstation i have 30 units of desktop, if im going to delete mp3 one by one its wasting my time, i just want to create a batch file that automatically and easily delete mp3 files… please help me to get rid of this.. thanks a lot

  11. can anyone give some help with this!!! please

  12. Alright, since i’m getting emailed every time you add a comment i’m going to intervene. Mr. Patterson is being very patient even though it’s clear that whatever problem you have is completely out of the scope of what his script is meant to do. Anyway, it’s really pretty difficult to figure out what you’re trying to do by your description. What exactly is a ‘unit of desktop’? I highly suggest you do a little more research (try google) before you ask any more questions. Here’s a hint: search for ways to find files in Windows Desktop Search. Using a batch script sounds like it might be dangerous for you.


Leave a comment

No trackbacks yet.