Delete duplicte files (ending with (1), (2), etc) in a folder

Hi let´s say I have a folder with 4 documents on it, but 2 of them are duplicates like:
Automation (1).docx
Automation (2).docx

How could I manage to delete all duplicate files with any kind of number in parenthesis?

Thanks and regards.

To begin with, you can use search pattern expression to pull these files into an array.
In this simple example, if your files have numbers they will end with a closing moon bracket “)” just before the “.docx” file identifier

string[] filesToDelete = Directory.GetFiles(myFolderPath, "*).docx");

For each file in filesToDelete
   Call File Delete Activity and pass it the file variable

A regex based approach

(From fi In New DirectoryInfo("C:\_RPA\_Demo").GetFiles()
Where Regex.IsMAtch(fi.Name,"\(\d+\)\.docx$", RegexOptions.IgnoreCase)
Select fi.FullName).toArray

ensure that following namespaces are imported:


  • we can prefilter to a extension as well
  • modify the regex pattern also to catch typically abc (1) Copy.docx cases
1 Like

It worked, I changed it so that it deletes every duplicate document with any type of extension, how could I add to delete the copy documents?
MyFile - copy.pdf

could go in this direction


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.