Open random excel file in folder

Hi all,

I want the bot to open a random excel file in a folder that does not contain the substring “BIS”.

Can someone suggest how to treat this?



Files = Directory.GetFiles("","*.xlsx")

Then iterate through For each loop

For each item in Files

Assign filename = path.getFileName(item).toString

if filename.contains(“BIS”)
ignore it
open file


Thanks for your reply.
Isn’t this solution going to iterate me through all files that do not contain “bis”?
I only want to open one random file.

use CInt(Math.Ceiling(Rnd() * yourArray.count)) + 1 to generate a random number

1 Like

After opening one file, use break activity to stop the process.

1 Like


Try like this,

Directory.GetFiles(dirName).[Select](Function(f) New FileInfo(f)).Where(Function(f) Not f.Name.ToLower.Contains(“bis”)).ToList()

Variable to be created:
strOutput As List(Of System.IO.FileInfo) = New List(Of System.IO.FileInfo)

This will return list of FileInfo… where you can get all the file names other than, which are not contains “BIS” in its name.

1 Like

Hello @yannip,

I guess that, @lakshman’s code is perfect for this.
please tell us that you wanna open all excel file from a folder where only file that contains ‘BIS’ must not opened.
Am i right?
you can use @lakshman’s code or attached file. OpenExcelFileFromFolder.xaml (12.7 KB)


1 Like

Hello @Pankaj.patil,

Thanks for your reply. No, the folder contains lots of files that do not contain bis. I will give you an example of how the names of files look like in folders:

  • 422_302_300.xlsx
  • 422_302_300_bis.xlsx
  • 120_540_0203.xlsx
  • 130_302_300.xlsx

I want to open one of the files that does not contain “bis”, so 1 out of those 3 others, randomly.

Could you maybe adjust the xaml where you think it is needed?

Many thanks!

Hello @yannip,

Thanks for supplying an example :).

I created a files with your given names kindly have a look at below snapshot,

Please find a modified xaml file.
OpenExcelFileFromFolder.xaml (11.5 KB)

Kindly keep us posted if you stuck :slight_smile:.


1 Like

Thanks for the input all.

I solved it by using the solution of @lakshman and by including a break activity to exit the for each loop once one file is opened.

However, for people looking for the same solution, it is not opening a random file but just the first file in the folder that meets the conditions you specify. For me that’s fine as well, but people looking for a solution to open a random file will not be satisfied with this solution.

Have a nice day

1 Like

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