Complex Filter for Directory Files

Saw this question here but unfortunately nobody was able to provide a good solution and I was not able to respond to the question with an alternative solution.

My suggestion is to use a regular expresion (regex) - you can test your regex here. Regex can be a bit confusing at first but it is a great skill to learn. This code also uses Linq (the part of the code which says .Where(…)) which is a very powerful method of doing inline code.

The structure in the package would go as follows:

1. Assign Activity - with VB code:
Create a variable of type System.Collections.Generic.List<System.IO.FileInfo> and assign the following code to it:

New DirectoryInfo(“C:\your_folder_path”).GetFiles(“*”).Where(Function(currentFileInfo) Regex.IsMatch(currentFileInfo.Name,“your_regex_pattern”)).ToList()

Modify ‘.GetFiles(“*”)’ with an additional parameter like SearchOption or EnumerationOptions for additional control - you also will need to use an .OrderBy(…) linq operation if you would like to order the files by one of the FileInfo properties such as name or last modified date

2. Foreach Activity:
Put your variable from the assign activity in the ‘List of items’ input
Now use this loop exactly how you would use a normal Foreach File in Folder activity.

I hope this helps someone!
Happy Automation everyone.

1 Like

Hi @matt.theisen ,
Could you please share your demo source
I think it helps someone with similar this issue
regards,

Hi,
I will create a small example and post it shortly.
Thanks

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