Delete files that have same name but different file extension. Keep only one of them

Hello everyone.

I have a folder with hundreds of files. Some files have the same name but a different file extension. These files contain the same information and therefore I consider them duplicates. However, due to the different extension, they can coexist in the folder because the system does not consider them duplicates.
Example:
redcolor.doc
redcolor.docx
redcolor.pdf
etc.

I want to find a way to scan through the folder and delete the redundant instances, and only maintain one file for each file name. I don’t have a preference on the format, I just want to keep only one of them.

My approach was to use a For Each file, then get the file name without the extension, and then inside that For loop use an If statement, where the condition is:

Directory.GetFiles(FolderPath, “FileName.*”).Length > 1

And if that is true, then delete the current file.

Clearly, that is not correct, as I am not getting the desired result. Any ideas?

Hi,

I attached a way to achieve that.

Main.xaml (8.5 KB)

What I did was group the files by its name (without the extension).

Then, I iterate through the grouping results keeping just the first index for each group, deleting the others.

Hi.
Good idea Alex.
I found a little bit different way. I created auxiliary data table and I searched for each any unique file name.
When I found more then one file name I removed each next one.