Getting files created in last 5 days from a SFTP server starting with a text

Bonjour,

Can someone please help in the below scenario?

I need to get the files from the SFTP server as per the following conditions:

Execution_202001007 - Folder
-> Contra_07_10_2020.xlsx - File
-> Report_07_10_2020.xlsx - File

Execution_202001006 - Folder
-> Contra_06_10_2020.xlsx - File
-> Report_06_10_2020.xlsx - File

Execution_202001005 - Folder
-> Contra_05_10_2020.xlsx - File
-> Report_05_10_2020.xlsx - File

Execution_202001001 - Folder
-> Contra_01_10_2020.xlsx - File
-> Report_01_10_2020.xlsx - File

Execution_202000930 - Folder
-> Contra_30_09_2020.xlsx - File
-> Report_30_09_2020.xlsx - File

  1. Only the files that starts with Report needs to fetched which are present inside the folder with name as “Execution_YYYYMMDD”

  2. Only last 5 created folders needs to be accessed.

  3. We can not apply weekend logic alone as there might be some exceptions for holidays in weekdays so we just want to access last 5 created folders and get the one file starting with “Report_”

Thank you in advance for the help!

Hello @alpharobot!

It seems that you have trouble getting an answer to your question in the first 24 hours.
Let us give you a few hints and helpful links.

First, make sure you browsed through our Forum FAQ Beginner’s Guide. It will teach you what should be included in your topic.

You can check out some of our resources directly, see below:

  1. Always search first. It is the best way to quickly find your answer. Check out the image icon for that.
    Clicking the options button will let you set more specific topic search filters, i.e. only the ones with a solution.

  2. Topic that contains most common solutions with example project files can be found here.

  3. Read our official documentation where you can find a lot of information and instructions about each of our products:

  4. Watch the videos on our official YouTube channel for more visual tutorials.

  5. Meet us and our users on our Community Slack and ask your question there.

Hopefully this will let you easily find the solution/information you need. Once you have it, we would be happy if you could share your findings here and mark it as a solution. This will help other users find it in the future.

Thank you for helping us build our UiPath Community!

Cheers from your friendly
Forum_Staff

Hi @alpharobot,
Have you tried to work with UiPath.FTP.Activities?

Yes, I am able to use that activity but my concern is related to getting the only files which are desired.

Currently, I am able to download the entire folder “Execution_YYYYMMDD” as shown above and then I am deleting the other files except the required one.

My main concern is with the “extracting the last 5 created files from inside the folder”
Note: We can not use directly the last 5 days as their are weekends and holidays

The Enumerate Object activity work on VB/C# (depends on your selection) expression so you can define it there I guess.

I am not able to apply that logic only, that how to filter only the Report_DD_MM_YYYY files of folder which are created in last 5 days.

Hi @alpharobot

The easiest way would be to loop over your files and only download the ones you need.

First thing, make sure you get the latest FTP package version:
image

Then, do something like below, just with your own custom logic, and actually downloading the file in the Then branch:

Make sure the type for the For Each matches the output of the Enumerate objects activity, this way you will get the IntelliSense tips about the properties of the elements in that list:


image

Thank you so much for the reply but my main concern is that how can we get only the files from the last 5 created folders.

I can’t just apply skip weekend dates logic as sometimes file might not be there due to others reasons as well.

I can confirm I am using the latest version of FTP and with correct variable type for the loop.

You could probably use the .Modified and .Created properties of the file to achieve that by sorting the list based on that and then picking the top most records. To be extra sure, you could first loop the list and remove the elements that you don’t want, or alternatively loop over it and add the elements you need to a new list.

Hi @loginerror

I need to get the last 5 created files.

But, as per your suggestion if I check the .modified property of all the folders that it will not be a good solution as there are more than 10k folders in there. This is why I am searching on how to just get the last 5 created files by some method.

Can we sort the SFTP directory by the date created/modified and get the top 5 folders’ name?

Please help!

Thanks a ton in advance!

I think you should be able to sort it with some LINQ expression, something like:


image

This still forces you to load up all files into the list variable. A solution here would be to group the folders on the server a bit, i.e. having them in separate folders by year and maybe by month.

Another way would be to dynamically generate the path to the folders and fetch one folder at a time, then check if any files were there (or if the folder did exist, it might throw an exception you would catch), and when you grabbed a file, add it to the list until the list has 5 elements.

Thanks @loginerror for the reply and help!

Linq think i will try but as you said it will required to load the folder in list first.

Creating dynamic names was an option I tried but the folders are not created daily, so in this case we can not simply consider last 5 dates and create dynamic folder names for them that might be the case last 5 days the folders were not created so then we will need to extract the previous ones.

Grouping the folder on the server is not possible as of now as it is up to business but thanks for highlighting this.

I will now surely try the Linq method to see the execution time.

Thanks a ton @loginerror, you are amazing! :v:

1 Like