How to get the first file stored in a folder?

Good afternoon!

I’m trying to develop a small solution to get the first file inserted into a particular folder…

So I can process them from the oldest to the newest.

This is what im using:

String.Join(“”, Directory.GetFiles(PastaDownloadDocumentos,“*.pdf”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1))

Those are my pdf files:
image

In this case, I want to process in the first place the file from 12:58, but with my expression, I’m getting the file from 13:08.

Sequence:

Any ideas on what is wrong?
Any help is well appreciated, thank you

Hi @Andre_Vieira

  • Just go to Activity panel ->click on filter → click on studio X

Just look it and let me know

2 Likes

Hello Goku, I really don’t know if I want a loop, because the loop will pull all files in the folder almost at the same time and I don’t want that because each file must be processed individually.

I just want the robot to go to the folder and pick the oldest file and that same file will be moved for a final location. So if i have a loop i will have to pull all the files at the same time and moved them all at the same time and process them all at the same time, and something will gone wrong xD

Hey

  • After the file get move to the final location you can use the “Break” Activity.
  • So it will be executed only once.
1 Like

Sunds like a plan, let me test this. I’ll give you feedback soon

Hi @Andre_Vieira
Sounds good

1 Like

@Andre_Vieira - Instead of creation time…use lastwritetime.date = today and take first …

1 Like

I’m getting some access issues now

It works, if I put a message box inside the loop, It will show me the path of the file that I want to process.

Don’t now

Hello, let me try

Hi @Andre_Vieira,

You then need the LastWriteTime for FileInfo

GetLastWriteTime

You can try this:

String.Join("", Directory.GetFiles(PastaDownloadDocumentos,"*.pdf",SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).LastWriteTime).Take(1))

You also have to be aware of this issue where the LastWriteTime can be older than CreationTime : c# - How can FileInfo.LastWriteTime be earlier than FileInfo.CreationTime? - Stack Overflow

1 Like

Hi

String.Join(“”, Directory.GetFiles(PastaDownloadDocumentos,“*.pdf”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).lastwritetime.date = today))

This is what o mean?

Hi @Andre_Vieira

  • Don’t change the properties in “For Each File in Folder” Activity
    - Also use CurrentFile in the Read Pdf Files
    Refer the screenshot

I think, so it will work. can you check screenshot ?

1 Like

Hello, I’m getting

What variable type did you use?

Try LastWriteTime

LastWriteTime instead of GetLastWriteTime.

1 Like

If I can’t find another way I’ll try it again bro. Because I prever not working with loops

Hi @Andre_Vieira

You don’t want to use loops in your Activity.
Loops are works better in my opinion.

1 Like

I’m using:

String.Join(“”, Directory.GetFiles(PastaDownloadDocumentos,“*.pdf”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).LastWriteTime).Take(1))

With this expression, the robot is processing the last pdf in folder, in terms of arrival.
And I want to process the oldest one. :sweat_smile:

You can do it in one line query without looping…I will show you when I get back…

1 Like

Hi @Andre_Vieira

String.Join(“”, Directory.GetFiles(PastaDownloadDocumentos,“*.pdf”,SearchOption.AllDirectories).OrderByAscending(Function(d) New FileInfo(d).LastWriteTime).Take(1))

Try this expression

1 Like

Hi,

That is the challenge as I said earlier with Creation and LastWriteLine when files are downloaded. What if you use Ascending? Do you get the expected, I am assuming it should because that will be the least distant time (last modified closest to Now).

1 Like