Looping Files in a folder and extracting filename based on month and Year

I have multiple excel files in a folder with following naming convention Month Year abc.xlsx. I need to select the file month wise. Like say I need Jan 2006 abc.xlsx file then in next run I need Feb 2006 abc.xlsx file and so on in next run. I am using
→ For each file in folder filter by *.xlsx

How can I achieve this ?
TIA

hi @raygandhit
there is an order by property in for each file file in folder activity.
Try using that

image

Also if order by property is not fulfilling your criteria you can try the below expression.

Directory.GetFiles(folderPath, "*.xlsx").OrderBy(Function(f)DateTime.ParseExact(Path.GetFileNameWithoutExtension(f).Substring(0, 8), "MMM yyyy", CultureInfo.InvariantCulture)).ToList()

Note:- The expression is from the top of my head I am not able to test it currently.

I tried the expression it did not work it gives an error

Whats the error and did you try the order by property?

edit: Seems to work for me

folderPath in the expression is a variable which will contain the path of the folder.

If you are getting cultureinfo error then use

System.Globalization.CultureInfo.InvariantCulture

image
Variable type is List[String]

yes declare a list variable. use an assign activity.

Then you would loop through the list the current item of the loop will contain the path of the file

edit: You can use the expression directly in a For each activity. if you are not familiar with data types

image

I still get this error
image

I have variable type as List

Can I get xaml for this please?

@raygandhit assign the query in for each activity
or send the entire screenshot what variable type are you trying to assign to the query and in the query you have got .xls it should be .xlsx based on your description

I used For each activity and changed type argument to System.Collections.Generic.List<System.String>

List of Item : Directory.GetFiles(“\is-test"),”*.xls").OrderBy(Function(f)DateTime.ParseExact(Path.GetFileNameWithoutExtension(f).Substring(“0,8”),“MMM yyyy”,System.Globalization.CultureInfo.InvariantCulture)).ToList()

@raygandhit in .substring remove (“0,8”) from double quotes it should be like (0,8)

edit: type should be object

Thanks for the help, It worked

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