Get the latest file from a folder using UiPath

A lot of times we use files while automating business processes.

Sometimes we download files from a web browser any other way and store them in a folder.

Now, in order to process that particular file that is downloaded, we need to get the latest downloaded file from that folder because there were so many files in that folder.

Let’s see the implementation of how to get the latest file from a folder using UiPath:

Step1: Drag and Drop an Assign Activity and provide the folder path where we are storing the files as shown below:

latest file from a folder

Step2: Drag and Drop an Assign activity and write the below code inside it:

String.Join(“parameter”,Directory.GetFiles(FolderPath,”*”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(n))

Here:

Take(n): defines how many file paths you require i.e., n=1 or 2 or 3, and goes on.

It will sort the latest file based on creation time from all files.

parameter- It will separate the file paths with this parameter.

If you enter ‘,’ there then the output will be like this filepath1, filepath2.

It will be like this we are combining all the file paths.

If you want only one latest file from a folder then follow as shown below:

latest file from a folder

Here we are taking only one latest file from the folder.

Step3: The output looks like below:

latest file from a folder

If you need multiple files from a folder then increase the n value inside the code like this Take(value).

This is a useful task when you are working with web automation and specifically working with downloading and uploading the latest files from a folder.

Happy Learning!

Like this post then let your friends know about this-:

Leave a Reply

Your email address will not be published. Required fields are marked *