Find latest File in a folder with respect to date Created

file
activities
variable
selection

#1

I have written a code to find the most recent file in a directory, it finds the most recent file as per the last modified date. The logic for the same is as follows:

Create an array of datatype File info and save the names of files present in the required directory in descending order with respect to their last modified date. The code for the same is as follow:

last_mod_files = directory.GetFiles("*AHS Roster.xlsx").Where(function(file) file.LastWriteTime.Date= DateTime.Now.Date).OrderByDescending(function(d) d.LastWriteTimeUtc).ToArray

now the first element of the array is assigned the required variable:

MyExcelFile=last_mod_files(0).ToString

This code works perfectly if any file inside the folder is modified on the same date as the bot is running on.

Example: File is modified today and the bot is run today too, it will pick the latest modified file.

But the bot throws an error if the file is modified yesterday and the bot is running today.

The bot throws an error where the array last_mod_files isn’t getting initialized. The assigne activity MyExcelFile=last_mod_files(0).ToString is throwing an error saying the value was out of range.

Changing the code and replacing LastWriteTime with GetCreationTime as shown below

last_mod_files = directory.GetFiles("*AHS Roster.xlsx").Where(function(file) file.GetCreationTimee.Date= DateTime.Now.Date).OrderByDescending(function(d) d.GetCreationTimeUtc).ToArray

gives the following Error :

Any Idea why it’s happening? How can I resolve the above error?

Main XML File containing the code: NewestFileIinDirectory.xaml (9.7 KB)

Reference Link : How to get File Created Date and Modified Date

Please guide me and suggest changes wherever required.

Thank you.


#2

@RishiVC1,

Obviously, if it has yesterday as a lastmodifieddate then if you compare with todays date then it returns an empty array. And if you try accessing it elements, probably you will end up in error.
Solution you can check the count of that array before you access or make any operation.

It should be file.CreationTime .
Reference

  1. How do I get the lastest file in the folder
  2. https://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.110).aspx

Regards,
Dom :slight_smile:


#3

Thank you @Dominic,

Isn’t that’s the whole idea, to compare the latest file in the folder with respect to today? I’m supposed to pick the file in the folder which was recently pasted there. The file could be pasted/created any day (on present day or any day before present day). How should I go about it, without comparing it to today’s date.

Please guide me .

Thank you .


#4

@RishiVC1,

This one solves your need. link that I have shared in the previous thread.

Regards,
Dom :slight_smile:


#5

@Dominic

I tried the above link, but I’m getting the same error as mentioned by other.

image

Could you please help me with that? I’m quite new to .NET. If possible could you please Update the attached XML or Guide me what changes I need to make ?

NewestFileIinDirectory.xaml (9.7 KB)

Thank you.


#6

@RishiVC1, That error occurs when you try converting a IEnumerable to string .
Also the attached xaml works fine. What error are you getting here. As a change i would suggest to check the last_mod_files array before you access it. Because it throws an error if there is no file.

Regards,
Dom :slight_smile:


#7

In my case, the file inside the required folder where the bot is looking for the latest file could be created/ copy-pated on any day (Not only the day when the bot is running). Out of the available files in that folder I need to pick the last file created in that folder irrespective of the day when the bot is running.

As per this post : How do I get the lastest file in the folder

The below code should give me the path of the file (Which is most recent ) right? It should be a string then, please correct me if I’m wrong. if not then how can I get the file name ?

Directory.GetFiles(folder_path,".xlsx",SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)


#8

@RishiVC1,

Use
String.Join("", Directory.GetFiles(folder_path,".xlsx",SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1) )

Because this Take,Skip are the methods of collection like IEnumerable,Array,String. So there will be a typecasting error if we implement it directly. Either we need to loop inside the collection or String.Join() does the work.

Regards,
Dom :slight_smile:


#9

Did you try? (First/Last instead of Take)

Directory.GetFiles(folder_path,".xlsx",SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).First


#10

With this, I’m getting File “Assign : Sequence contains no elements” error. Could you please have a look at attached XML in the question.


#11

I’m getting blank printed with no error message. :neutral_face:


#12

@RishiVC1, Sorry dude there was (*) missing before .xlsx

Regards,
Dom :slight_smile:


#13

Thank you so much. I should have realised the same after getting a blank screen. :sweat_smile:
Could you please forward me the link where I can read more about the method used.

For anyone looking for a solution for the same problem, the final code is :slight_smile:

String.Join("",Directory.GetFiles("FolderPath","FileType/FIleNameSequence",SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1) )

where
FileType is the type of file you are looking for

Example:
You could be looking for latest excel file in the folder. so the File type should be replaced by “*.xlsx”

FIleNameSequence Is the naming Sequence of the file (in cases where file has fixed part and a variable part:

Let’s say there are many files in the folder and you are looking for txt files which are named as “XXXx_abc.txt” then the FIleNameSequence should be replaced by “*abc.txt”

NewestFileIinDirectoryAnyDay.xaml (6.6 KB)
TodaysNewestFileIinDirectory.xaml (10.7 KB)
LatestDirbyDateCreation.xaml (5.3 KB)

Thanks @Dominic and @vvaidya


How to get the latest file in a folder
How to get the image -Date Taken from UI Path?
#14

@RishiVC1, TBH I have learnt these from forum and you can refer MSDN Site for more details.

Regards,
Dom :slight_smile:


#15

@RishiVC1, you can mark this one as a solution, Latest File in a folder

its more descriptive…

Regards,
Dom :slight_smile: