How do I get the lastest file in the folder

I have a folder with three files tes1_v1.xls, test2_v2.xls, test3_v3.xls. test3_V3.xls is the latest file.

How do I move the latest file to a new folder.

2 Likes

Directory.GetFiles(folder_path,“.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)

8 Likes

Could you please explain what is “OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)”

Sorts files by creation time and takes top 1 file

What is “(Function(d) New FileInfo(d)” …

I have two files in the folder-

I have used the statement-
Directory.GetFiles(Filepath,“100”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)

I am getting this result -

Please help !!

Try 100*.xlsx

getting same error !

That doesn’t look like error…try getfile(0).ToString

did not work :frowning:

It works with

Directory.GetFiles(folder_path,“.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Tolist(0)

2 Likes

Hello All,

Can you please add the XAML file, I am in same situation, your help is appreciated.

Thank you!

I am travelling and don’t have access to the system for XAML.

But see if this helps.

dir = Directory.GetFiles(folder_path,“.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).Lastwritetime)

msgbox dir (0).
— dir is IENumerable variable

@sampaddas :
I tried the suggestions mentioned here,
for Directory.GetFiles(folder_path,“.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Tolist(0), I got indexoutofrange exception.

for Directory.GetFiles(folder_path,“.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1). I got System.Linq.Enumerable+d_24`1[System.String] error.

Can you please suggest me a way out of this?

dir is ienumerable = Directory.GetFiles(“Folder_Path”,“File_Name”,SearchOption.AllDirectories).OrderByDescending(Function(x) New FileInfo(x).LastWriteTime)

1 Like

I m getting below error

‘getfiles’ is not a member of system.collection.IEnumberable.How to rectify this.help plx

Assign activity:
directory = Directory.GetFiles(,“task_sla.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Tolist(0)

directory is in IEnumerable datatype