Searching for files from the last month


I’m trying to find .xlsx files from the last month and move them to another folder. I tried to find a solution, this is what I got so far…

timecheck (as string array) = Directory.GetFiles(“C:…\Documents\TESTfolder”, "file_.xlsx").Where(Function (a) FileInfo(a).LastWriteTime.month NotEqual Now.month)*

whats wrong with it ?

1 Like

Right now it is getting it based on the last write time. You want that, correct? Just want to be sure you don’t mean the creation date instead. Also, it is grabbing all files that were modified in any month besides the current month. So it would be grabbing things modified 2+ months ago as well. I am assuming that’s what you want, please correct me if any of these assumptions are incorrect

Update it to this and it will work fine: timecheck (as iEnumerable<string>) = Directory.GetFiles(“C:…\Documents\TESTfolder”, "file_* .xlsx").Where(Function (a) File.GetLastWriteTime(a).month <> Now.month)

If you want to use the fileinfo way you can certainly do that too. Just change it to this: timecheck (as iEnumerable<string>) = Directory.GetFiles(“C:\Users\azl6ibu.AZLIFEM\Documents\UiPath\Address Changes Processor”, "*.xaml").Where(Function (a) new FileInfo(a).LastWriteTime.month <> Now.month)

If you want to check for ONLY the previous month, then you should modify it so instead of .Month <> now.Month it instead says .Month = now.AddMonths(-1).Month

1 Like

Fine, this wont give us the file created in this month.
We need the file created only in the last month and the above expression doesnt mean that we will be getting only last month,
may be from two previous months or from even before that, though not from current month for sure.

so to get only the files from last month, the expression be like

arr_FilePath = Directory.GetFiles(“yourfolderpath”,"*.xlsx").Select(Function(a) New FileInfo(a).LastWriteTime.Month = Now.AddMonths(-1).Month)

where arr_FilePath is a variable of type string array

hope this would help you
Cheers @B.D

1 Like

Kindly let know for any queries or clarification
Cheers @B.D

Hi Palaniyappen,

I later came to the conclusion that searching for any file older than the crrent month would be enough for my needs (since I didn’t know the exact method to search for files of the last month). I tried your method but it retiurns the error that it can’t be converted into string array. how can I select the type ienumerable (of boolean) for the variable ?


thanks a lot. Now I want to add the year, because it left a single file (september 2018) out, because it is from this month. but when modify it to: … <> now.month and now.year) it doesn’t work with the ienumerable type because of the boolean operator and. is there a ienumerable (boolean) type ?

IEnumerable is a collection of items. So it is similar to an array, list, etc. In this case, it as an ienumerable of string (which is what you want) because you are enumerating over all of the file paths.

The reason why it didn’t work when you changed it to … <> now.month and now.year) is because the and operator isn’t comparing anything on the right side. Every time you add an OR or AND statement, you need to compare 2 specific booleans. Right now you’re checking the file’s modification month to the current month. But when you put in AND in there, you need to specify again that you want to check the file’s modification year in comparison to the current year. So you need to instead put:

timecheck (as iEnumerable<string>) = Directory.GetFiles(“C:…\Documents\TESTfolder”, "file_* .xlsx").Where(Function (a) File.GetLastWriteTime(a).month <> Now.month AndAlso File.GetLastWriteTime(a).year <> Now.year)

NOTE: I used AndAlso instead of a simple AND. AndAlso (along with OrElse instead of Or) is called lazy evaluation. This means it will check the first statement and only continue if it is true. If you use a regular AND, then it will check all statements even if it finds a false right away. I use AndAlso (and OrElse) 99% of the time because this can save a decent amount of processing time depending on your workflow.

if use the extension it doesn’t seem to work the workflow is according to Ui path successfully completed but no file is moved. When I remove AndAlso File.GetLastWriteTime(a).year <> Now.year the first part does work again. Any idea ?

Oh yes I didn’t think about that. It’s a logic problem haha. As it’s currently written it will only find things older than 1 year that are not in the same month. We may need to do checks, note that the parentheses are very important here:

timecheck (as iEnumerable<string>) = Directory.GetFiles(“C:…\Documents\TESTfolder”, "file_* .xlsx").Where(Function (a) File.GetLastWriteTime(a).month <> Now.month OrElse (File.GetLastWriteTime(a).month = Now.month AndAlso File.GetLastWriteTime(a).year <> Now.year))

1 Like

awesome! it works! thank you very much. I have one last question regarding palimiyappen’s solution. I also tried his but Ui path returns that I have to use ienumarable (boolean). why that ? and where can I find this type ?

PS: thank you for the help and explanations, I learned a lot today :slight_smile:

@B.D It’s because @Palaniyappan is written slightly incorrectly based on your requirements. Right now it is using a SELECT statement, which means it is using the lamda statement (the inline functions) to the right of it, and returning the results of that lambda statement. Since it is comparing a whole bunch of booleans, it is then returning all of those booleans in an ienumerable.

However, we don’t want to SELECT the stuff on the right, instead we want to use the stuff on the right as a filter. That’s why we want to use a WHERE statement instead. WHERE acts differently in that it uses everything the lamda statement as a filter (so it requires a boolean, and it only returns values stated to the left of the where clause when the boolean on the right side of the where clause is true). Both the SELECT and WHERE statements return an ienumerable type, but we can change it back into an array by simply appending .ToArray() at the end of the statement.

I hope that helps, I can try stating it in a different way if it still is not clear :slight_smile:

1 Like

Oh ok I got. select is basicly returing what the operators do in this case month but doesn’t return the filepath of the conditions we are searching for. there is a whole lot of theory behind, but at the end it’s all logical. I’m now 2 weeks into Ui path there is really an aweful lot to learn. do you have any recomondations (books etc.) wuth diffrent cases and solutions, except the video tutorials here ?

1 Like

Yes that’s it exactly :slight_smile: . It’s hard to say what to recommend because UiPath can absolutely be used without this type of stuff. Things like LINQ/Lambda (which is what all this .select() .where() function(x) nonsense is) definitely isn’t required within UiPath. It does make a lot of things much easier though :slight_smile:

If you have a grasp on the basics of programming such as variables, loops, if/then/else, try-catch, etc and you’ve already completed the entire uipath training + advanced training courses, then I’d say your best bet is to just practice as much as possible. Try and help out as many people on the forum as possible. Just attempt to solve the issues, even if you aren’t able to do it at first it’s worth the try as you’ll learn a lot in the process.

Google is also your friend. If you’re struggling with a concept or error, it is 99% likely that someone else has had the exact same issue and has already posted about it on the internet. Sites like stackoverflow are a goldmine. The trick is being able to use the stuff you read there and translating it into UiPath format which just comes with time & practice.

1 Like

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