Keep only the newest files with name alike but not the same

Hello everyone!

I have to process these documents to delete the first dwg files if the is another updates of the files in folder like -01, -02.
For example for 100-00011815730001- i have to keep only 100-00011815730001-02 and delete the -01 and -. Same for 100-00011815730002- .

Thank you!

Hi @Andrei_Croitoru

As a whole you need only the distinct files right?

Regards

Yes, it have to result only -02.dwg files for each. But sometimes can be an -03, -04 files

Hi @Andrei_Croitoru

Need Clarification you need only the files that are ending with 02 or what?

Regards

Hi @Andrei_Croitoru

As per my understanding as you need to keep only the files which has the highest number ending !

I am right?

Regards


I have the ID: “100-00011815730001” I want to keep in folder only the “100-00011815730001-02” version of it because is the newer.
The same for ID: “100-00011815730002” I want to keep in folder only the “100-00011815730001-01” version of it because is the newer.

Hi @Andrei_Croitoru

Seems bit tricky!

Tagging @ppr @Yoichi to have a view on this!

Regards

will have a look on it soon

1 Like

Hi,

Can you try as the following?

arrFile = arrFile.GroupBy(Function(f) System.IO.Path.GetFileName(f).SubString(0,18)).SelectMany(Function(g) g.OrderBy(Function(f)  System.IO.Path.GetFileNameWithoutExtension(f).SubString(19).PadLeft(4,"0"c)).Take(g.count-1)).ToArray

Regards,

1 Like

Was working on a regex / FileInfo / SetOperation Approach with a sample set
Vars:
grafik

Flow:
grafik

arrAllFileInfos = New DirectoryInfo("C:\_demo\FileSystem\GetLastDemo").GetFiles("*.txt")

arrKeepSet =

(From fi In arrAllFileInfos
Let cr = Regex.Match(fi.Name,".*(?=\-)").Value
Group fi By k=cr Into grp=Group
Let grpo = grp.OrderBy( Function (x) CInt("0" & Regex.Match(x.Name, "(?<=\-)\d*?$").Value))
Select f=grpo.Last()).toArray

arrDeleteSet = arrAllFileInfos.Except(arrKeepSet).toArray

Visuals:

Last Line is showing on how to get a String Array with FullFilePaths

Adopting it to your case we would suggest:

  • shift to dwg extension
  • incorporate variables e.g Directory where the files are located

Find starter help here:
FilesWithAddedCounter_GetLatest.xaml (5.6 KB)

1 Like