Delete duplicate files with Windows batch file

I have a program that automatically copies files to a directory, and if it creates a duplicate it will name it like so:

file with duplicate.xxx
file with duplicate - 1.xxx
file with duplicate - 2.xxx

I need a way to delete all the duplicates with a Windows batch file.

Something along the lines of:

FOR %f IN (C:\files\*.*) DO del "%f - 1"

However, that will not work because that would resolve to file with duplicate - 1.xxx - 1


If you want to delete all the files with a name that ends in - ?, then you were close.

Try this:

FOR %%f IN ("C:\files\* - ?.*") DO del "%%f"

You will need to double up the percent signs if you are running it from a batch file. And test with an echo instead of a del first.


No need to the FOR, del can accept wildcards. Try:

del "c:\files\* - 1.xxx"

Will match anything with the suffix " - 1.xxx" in the directory.