How to get the first 4 files in a directory

guys, need help, how to get the current 4 files in a network directory and copy in local drive.

Hi @Henson_Ibon … Please try the below code…

StrArrFirstFour = Directory.GetFiles("YourFolderPath").OrderByDescending(Function(d) New FileInfo(d).LastWriteTime).take(4).ToArray

Here StrArrFirstFour is StringArray

Hope this helps…

1 Like

thank you.

when i copy the files and I want to rename it in the local folder to for example

file#4 = mod1.txt
file#3 = mod2.txt
file#2 = mod3.txt
file#1 = mod4.txt

@Henson_Ibon - Yes, please try as below…

Index = idx is Int32 variable (it starts with 0)

In the Copy File

"DestinationFolder\" + "Mod" + (idx+1).tostring + path.GetExtension(EachFile)

Hope this helps…

I see you want the name in the reverse order right? Then in that case …try the below

 "DestinationFolder\" + "Mod" + (4-idx).tostring + path.GetExtension(EachFile)

thank you. already got the desired output.

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