Windows – Removing duplicate files through batch files or Windows 7

windowswindows 7

I have a blended music library with both “mp3” files and “m4a” files. Many of the music directories and specific album sub directories actually include what appears to duplicate music song files differing only by the file extension of both mp3 and m4a files.

For those directories that contain the same songs with both extensions, how can I use Windows 7 and/or a batch file command to search through my entire music directory (and respective sub directories) and delete only those m4a files where the same song file exists in the mp3 file format in the same music library folder as well?

My music library contains about 75,000 songs if I ignore the duplicate m4a files of the same song that may exist in the same music folder(s) of various albums.

However, right now, with no regard to specific file extension types, my directory appears to have over 100,000 songs (where obviously 25,000 of these songs are duplicates that account for these aforementioned file formats of the same song(s) coexisting in their given respective music album folder and/or music sub directory(ies)).

What operation or command can I use to safely scan my music library and remove extraneous “m4a” song files where the preferred “mp3” files already exist?…While KEEPING those m4a files where NO same song mp3 files already exist?
I also need step by step instructions on how to setup and start any batch files that achieves the stated goal above. Thanks so much Guys!

Best Answer

Here’s a script (a.k.a. a “batch file”) that will do it for you in case you don’t want to download a program from somewhere:

@echo off
setlocal enabledelayedexpansion
for /r %%F in (*.m4a) do (
    set mp3name=%%~dpnF.mp3
    if exist !mp3name! (
        fc/b %%F !mp3name! > nul
        if errorlevel 1 (
            echo Warning: %%F and !mp3name! both exist but are not identical.
        ) else (
            echo DEL %%F
        )
    )
)

Interpretation:

  • for %%letter in (wildcard) do some_command scans the current directory for files matching the wildcard and executes the command with the variable identified by the letter set to the filename.
  • for /r %%letter in ... is the same but recursive.
  • () lets you specify a block of commands instead of just one.
  • As indicated above, %%F is the name of the M4A file that was found.
  • %%~dpnF is the drive letter, path, and (base) name parts of %%F –– i.e., all but the extension.  To this we append an .mp3 extension; i.e., a filename that is the same as %%F except it has an .mp3 extension.  Assign this name to the mp3name variable.
  • If this file exists, do the next six lines.  (Note that we are again using () to specify a block of commands.)
  • fc/b %%F !mp3name! > nul –– fc is file compare.  fc likes to compare text files, so we say fc/b to do a binary comparison.  So fc/b %%F !mp3name! compares the two files.  > nul means don’t display the results of the comparison on the screen; we only want the script to know the result of the comparison.
  • if errorlevel 1 means do the following command(s) only if the immediately preceding command “failed”.  In the case of fc, “failure” means that the files were not identical, so we have the script tell you that Hey_Jude.mp3 and Hey_Jude.m4a both exist but are not identical.
  • Finally, DEL %%F means delete the file.  (echo DEL %%F means display the delete command, but do not execute it.)

Setup:

  • In Windows Explorer, go to the root (top) of your music directory, do New Text file, and give it a name.
  • Double-click on it to open (edit) it in Notepad. Copy and paste the above script.  Save and exit.
  • Back in Windows Explorer, rename the Text (.txt) file to have a .bat extension.  Ignore the warning; click on “Yes”.
  • Start a Command Prompt.  cd to the above directory.  Type the name of the script (batch) file (you may type the .bat extension, but you probably don’t need to).  Verify that the list of DEL commands it reports looks reasonable.  If it does,
  • Go back in Windows Explorer, right-click on the .bat file, and select Edit.
  • In Notepad, change echo DEL to DEL.  Save and exit.
  • Back in Command Prompt, run the command again.
Related Question