Multiple extensions for for each file activity

Hi All,

I have folder it has multiple files with different extensions. I wanted to filter only those files having extension .xls ,.txt ,.pdf irrespective of name of file. I am trying to setup the filter for it in activity for each file in folder. But control is coming out of the for each. I anticipated there is an problem in setting filter query. Can some one help me with how to set this filter query to filter out multiple extensions.

Appreciate for sharing approaches and thoughts well in advance.
Thanks

Hi @shivappa.katti1

You can refer this thread

@shivappa.katti1

welcome to the community

you can use like this "*.txt" for single

cheers

Cant we add filter only instead of checking in for each. like, setting up the filter only to give only files with required extensions?

Hi @shivappa.katti1

Try this way,

If condition =

System.IO.Path.GetExtension(CurrentFile.ToString).Equals(".xls") Or
System.IO.Path.GetExtension(CurrentFile.ToString).Equals(".txt") Or
System.IO.Path.GetExtension(CurrentFile.ToString).Equals(".pdf")

Regards,
Gowtham K

I’m pretty sure you can do multiple by using a pipe. Try something like: “.xlsx|.txt|.csv”. Maybe with the * in front of the extensions

1 Like

Filter property in the For Each File in Folder activity does not support multiple extensions directly. So you will have to filter file using some other method as mentioned in the thread.

Unfortunately it doesn’t work like this. Filter accepts only single file extension :frowning:
Cheers

Hi @shivappa.katti1 ,

Welcome to UiPath Forum.

You can write a simple expression as mentioned below in assign activity and store it into an array of strings:

Directory.GetFiles(“FolderPath”).Where(function(f) f.EndsWith(“.xls”) OrElse f.EndsWith(“.pdf”) OrElse f.EndsWith(“.txt”)).ToArray

Next, you can loop through this array using foreach loop to read the intended files.

Please let us know if it answers your query.

Thanks,
Anjani

Just tried it out, you are right. I’m sure I have used this method for other activities with files and those worked perfectly. Shame it’s not supported on this one.

Hi @shivappa.katti1 try the below expression to filter
files = Directory.GetFiles(“YourFolderPath”).Where(Function(f) f.ToLower.EndsWith(“.xls”) Or f.ToLower.EndsWith(“.txt”) Or f.ToLower.EndsWith(“.pdf”)).ToArray()

Hie @shivappa.katti1 can you try that logic hope it help you to resolve your query .

  • here (folder location) [variable of string] holds the location of file data.
  • and (selectedFiles) [variable of Array of string] use to retrieve the desired files from the folder.

cheers Happy Automation.