How to get the latest file from the Download Folder

Hi team,

I know this question appears several times in the forum, but no solution I see solves my problem.

I want to automate the process of analyzing files (in this case, excel files). First, I download the file to my Downloads folder. Then, the idea is to start the automation.

I want the automation to take the latest file downloaded into that folder and work on it. I do this.

image

Where folderPath = userProfile + "\Downloads"

and latestFile = Directory.GetFiles(folderPath, "*").OrderByDescending(function(a) new FileInfo(a).CreationTime).First

It actually works, but the problem is that when I download the file, sometimes, there’s another file in the folder that has a latest “Date modified” and the excel file that I want to work on doesn’t end up at the top of the folder.

This sounds more like a Windows issue, but I wanted to know if anybody has gone through this in the past and knows a way around it.

Thanks

Hi @mardoza

Try with this expression

Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+"\Downloads").[Select](Function(x) New FileInfo(x)).Where(Function(f) f.Extension.Equals(".xlsx")).OrderByDescending(Function(x) x.LastWriteTime).Take(1).ToArray()(0).ToString 

Regards
Gokul

I saw that solution in the forum. It works for the first file that appears in the folder. The problem is that when I download from Outlook, the file doesn’t go to the top of the folder.

Where the file displays when you’re looking at the folder is irrelevant. That just depends on how you have the folder sorting set in Windows Explorer. The Directory.GetFiles expression doesn’t care how you have Windows Explorer sorted.

Which makes it even weirder because it always takes the file at the top of the folder. It does not care what I just downloaded. If it isn’t first in the folder, it won’t use it.

A workaround I found is actually open the excel file from Outlook and then save it. But I was hoping for some kind of explanation of why that happens. I’m learning.

Files have more than one date tag. One is creation date, one is last edit. You need to pay attention to which one you’re using in the Directory.GetFiles expression. I can assure you, it doesn’t care how you have Windows Explorer sorted.

1 Like

Thank you for the suggestion

I also believe that CreationTime should work, but you can try with sth like that:

In both first two assigns code is the same:

Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads"),"*").ToList()

In the assign for the newest file:

lst_FilesAfter.Except(lst_FilesBefore).First

So idea is to grab all the items from download folder just before downloading file. Then take all the items after downloading. Then remove from the second list all the duplicates. You should be left with one element which is the one that you are looking for.

That code will not wait for the file to be completed so you could do your own magic or just do it like that:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.