Read first item in an array, select the file whose name contains specific keywords

Dear all,

I wonder if you could help me with the application here in attachment. My Robot is supposed to
1- sort the folders by name,
2- pick the first one (which is the name with the most recent date),
3- find that specific Excel file inside this folder whose name contains a keyword (e.g. Industry, Banking etc)
4- do operations with this excel.

Well, I managed so far to complete point 1 and end up with an array of folder names sorted by name. 1. How can I now select the first folder in this array?
2. How can I search the Excel file in this folder whose name contains my keyword and store the full path of such selected Excel file in a variable for subsequent use? I tried with an if activity but it does not really work.

Thanks a lot

GennaroSales Meetings.zip (20.4 KB)

2 Likes

Hey @Gennaro_Bozza
please find the attached sample: SAmple.xaml (7.8 KB)

DirectoryInfo dirinfo= new DirectoryInfo("F:\2018");

DirectoryInfo first_sort_dir_name =dirInfo.EnumerateDirectories().OrderBy(function(d)  d.Name).OrderByDescending(function(s) s.CreationTime).First;

first_sort_dir_name.Tostring()// will return you the first sorted directoryname.which is the name with the most recent date

IEnumerable<String> files = Directory.EnumerateFiles(first_sort_dir_name.FullName, "*.xls*", SearchOption.AllDirectories).Where(function(s) s.Contains("Industry") Or s.Contains("banking"));

Regards…!!
Aksh

2 Likes

Hello @aksh1yadav, thanks a lot, that’s exactly what I was looking for. Amazing!

Is there a way to make it case insensitive? Such as it does not matter whether it’s “Banking” or “banking”? This would make the program more robust.

To be even more specific, what I will do will be replacing your s.Contains(“Industry”) Or s.Contains(“banking”)) with simply s.Contains(industryType) where industryType is my variable that is read somewhere else in my program. Now, can we make industryType case insensitive?

Many thanks

Gennaro

Hey @Gennaro_Bozza

just convert the name to lower case like this in both

s.ToLower.Contains(“industry”)

Regards…!!
Aksh

Hi @aksh1yadav,

many thanks for your reply. As I was adjusting your code to make it fit with my application, I discovered that an error is returned when I try to use the keyword as a variable.

In particular, an error is returned when I use the string variable “industryType” in place of “Banking” or “Industry”.

Please find in attachment your code in which I have added such variable and in which I was able to reproduce the very error that I get.

FYI, by googling this error I discovered that it might potentially be connected to a .Net Framework bug. There must be a workaround though I am still trying to find it.

Futhermore, as a side note, such variable can be either uppercase or lowercase depending on what the user writes. Also the keyword in the files can be upper or lowercase. As a result, your code “toLower” does not work if the variable is uppercase. How can we fix this? Is there a command similar to “toLower” also for the variable?

Many thanks

GennaroSales Meetings.zip (28.5 KB)

Hey @Gennaro_Bozza

It will work what i was doing is whatever filenames we are getting we are converting it all letters in upper case to match with our word so even if you will pass INdustryType or something code will match like this industrytype.contains(“industry”)…

For me it is working in all above mentioned case by you :slight_smile:

REgards…!!
Aksh

Hi @aksh1yadav,

thanks for your reply. Great to know about the toLower. As for the variable, did you see my .zip attached? Can you see that the “Banking” string is read from the Excel folder called “Industry selector”?

Are you saying that you don’t get the error message here in attachment?

2018-02-02%2016_31_22-UiPath%20Studio%20-%20Sales%20Meeting

Thanks

Kind regards,

Gennaro

Hi @aksh1yadav any news?

Thanks

Gennaro

Hi @Gennaro_Bozza,

In Sort_Directories Xaml.

before for loop assign activity value need to modify please use blow code.

Assign files=
Directory.EnumerateFiles(first_sort_dir_name.FullName, "*.xls*", SearchOption.AllDirectories).Where(function(s) s.Contains("industryType") Or s.Contains("banking"))

Regards,
Arivu

Hey Busy with Other Stuff that is problem with Ienumerable collection so will check for that in free time till then use the array approach. convert them into an array by using .ToArray() extention method.

Get Outlook for Androidhttps://aka.ms/ghei36

Thanks to everybody. I managed to fix it with a slightly different workaround. Basically I listed all of the keywords as you initially suggested, but then I added an if condition (if item.contains(industryType)) in the for each loop.

Thanks

Kind regards,

Gennaro

Hi @aksh1yadav

I came across a new hurdle that maybe could be an easy one for the UiPath community. My application is supposed to sort the folders by name, pick the latest folder (i.e. the first folder from the top) and read a specific Excel file in such latest folder according to the input.

Well, what I want to add now is the following: if such specific Excel file is not present in the latest folder, then search in the “second latest” folder.

In attachment, a working application in which the file “Insurance” is missing from the latest folder but is instead present in the second latest. Certainly the right command, that I haven’t managed to find yet, must be put in the “else” box of the if function in the code.

Thanks in advance to the community for your help.

Kind regards

GennaroSales Meetings.zip (39.0 KB)

Hi @aksh1yadav any chance you could suggest some ideas?

Thanks

Gennaro