How to I open the latest created excel file in a folder which the folder is in another folder

Hi all, in my case, I have a folder that contains 12 folders (which one represents one company).
In those 12 folders, I have several excel file downloaded from the website.
My task is to open the most recent excel file for each of the folder.

I use ‘for each folder in folder’ activity at the beginning, but I’m lost and don’t know how should I do to get the file in each folder when the folder is in another folder.

Hope someone can help…

That means, I have 12 folders in a folder, which contains several excel files in each of the 12 folders.

Hi @RPA_Innovation ,

  1. Use a for loop to loop through the folders by giving the main folder as Input.
  2. Inside the for loop use the following expresssion Directory.GetFiles().OrderByDescending(function(s) System.IO.file.GetCreationTime(s)).First() to get the latest file. Path will be the for loop item (which is the folder path of each of 12 folders inside main folder)

cheers

So I just need to use 2 for loop instead of ‘for each folder in folder’ activity?

  1. Directory.GetDirectories() will give you all folders as array
  2. Then loop through array using for loop
  3. In the for loop add assign and use Directory.GetFiles().OrderByDescending(function(s) System.IO.file.GetCreationTime(s)).First() this to get the latest file path.

Instead of 1 and 2 you can use for each folder in folder as well

So you need to use only one for loop for the directories

cheers

sorry, do you mean this?
image

What is the reason of my error?

Try this.

Get First Files.zip (3.1 KB)

Cheers.

Hi @RPA_Innovation ,

in for each change the type argument to String, and also replace the value in for each with Directory.GetDirectories()

cheers

image
It seems that the error is in ‘assign’ activity

Hi @RPA_Innovation

You have to change the In Argument not the Item

cheers

hi, I’m a newbie to automation so maybe I need more information for this

really appreciate your time

Hi, thanks for your example.
I tried to use it but it seems there’s some error too.



May I know how to fix the error in image 1?

Thanks a lot.

Hi @RPA_Innovation ,

Use it like this
image

In Assign give Directory.GetFiles(file).OrderByDescending(function(s) System.IO.file.GetCreationTime(s)).First()

cheers

Directory.GetFiles(“FolderPath”,“*”,SearchOption.AllDirectories).OrderByDescending(function(f) New FileInfo(f).CreationTime).First()

Have you assigned your folder path in Str_parentPath?

Hi @RPA_Innovation

Please check the following videos about getting the newest file from a directory in different methods or also how to loop files in different folders and subfolders:

sry I missed your reply…
image
U mean the second assign right? Yes I have

This is my error


Could you help?

and also, this is the error detail: [compare.xaml: Compiler error(s) encountered processing expression “CurrentFolder.GetFiles(Str_directoryPath,”*.xlsx").OrderByDescending(Function (p) p.LastWriteTime)(0).FullName".(2) : error BC30518: Overload resolution failed because no accessible ‘GetFiles’ can be called with these arguments:
‘Public Overloads Function GetFiles(searchPattern As String, searchOption As SearchOption) As FileInfo()’: Option Strict On disallows implicit conversions from ‘String’ to ‘SearchOption’.
‘Public Overloads Function GetFiles(searchPattern As String, enumerationOptions As EnumerationOptions) As FileInfo()’: Value of type ‘String’ cannot be converted to ‘EnumerationOptions’.]