I’m trying to get the latest 2 excel files from a folder that contains other pre-existing excel files.
String.Join(“,”,Directory.GetFiles(FolderPath,”*”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(2))
, it able to sort out the file names of the 2 latest files but the output is 1 consolidated file path(C:.…\Book1.xlsx,C:.…\Book2.xlsx).
I need it to be 2 individual file paths so I can proceed the for each activity.
the reason is you are using string.join to join the arry of file path so you are getting on string separated by a “,”.
to proceed with for each you need not use string.join, instead u can assign this
Directory.GetFiles(FolderPath,”*”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(2)
to a array variable and iterate with for each
Thanks for your prompt response, I had removed the String.Join and also create an array variable.
But I’m getting this error, am I short any keyword in the expression?
Because it doesn’t see the quotation marks. Delete the quotation marks in the code you copied from the forum and rewrite it. You should also have a variable called FolderPath. Define the file path of the folder you want to check.
Also, if you are going to use it in a loop, below
Add the specified .ToArray.
Directory.GetFiles(FolderPath,"*.xlsx").OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(2).toArray()
just append toArray() on it
@ppr & @muhammedyuzuak ,
Thanks!! It worked like charm.
I have some questions, sorry if these questions sound silly…
String.join method is not ideally applicable for files more than 1 ?
What is the difference between “toArray” and “toArray()” ? *
Both are working in my case
There is a log message to show the file’s name through the ArrayVariable, however, current output is showing “System.string
” instead of Book1.xlsx…Bokk2.xlsx…Is it normal?
Again,really appreciate your time and effort in replying, thanks in advance.
we wanna keep the array for the later looping with for each
toArray() is the correct naming the method, only toArray is a lazy short form (try to avoid it)
toString is not always implemented in a way that it will expose the variable content, in this case it is just printing the datatype (as often in also other cases)
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.