Windows – How to Delete Duplicate Files with a Batch File

batchcommand linewindows

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

Best Answer

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.