How to get latest file from ftp without looping

I want to get the latest file with a specific name from ftp and download. I dont want to loop it multiple times as it is not a better approach. I guess we can do it using Regex or LINQ. I tries can anyone help on this.

Files in FTP:

Expected output: for each file (ABC, PQR etc) i want latest file.

Could you put them into a collection or array and count with assign the last one?

Hi @raymondhui

I have it in lEnumerables
using the count and taking highest or lowest not providing expected output.

im exploring the other options like Regex or LINQ

Therefore you put it into a list? or able to split it?

My idea is to split these into a list and get the last one

Hi @sunilch

I can see there are multiple files in the same latest date. What is your expected output?

Hi @aanandsanraj

i updated my expected output


We can do it with the help of group by on the code and LINQ
assumption IEnumerable(Of String)

(From x in yourIEVar
Group x by k=x.SubString(0,3) into grp=Group
Let lst = grp.OrderBy(Function (x) DateTime.ParseExact(x.Substring(4,8),"yyyyMMdd", CultureInfo.InvariantCulture)).Last()
Select f=lst).toList

ensure System.Globalization is added to the imports

Hi @ppr

My IEnumerable is FtpObjectInfo

give a try on

(From x in yourIEFtpInfoVar
Group x by k=x.Name.SubString(0,3) into grp=Group
Let lst = grp.OrderBy(Function (x) DateTime.ParseExact(x.Name.Substring(4,8),"yyyyMMdd", CultureInfo.InvariantCulture)).Last()
Select f=lst).toList

should return a list(Of FtpObjectInfo)

Hi @ppr

I’m getting an error “Assign: Index and Length must refer to a location within the string. Parameter name: length”

Is it because i have few files with different length & types ? and we are relying on length. If that is case as alternate can write a query based on filename (i will provide) it should check provide me the latest file from list.

we are bound on a pattern. From above we used first Capitol Letters last 8 digits

So first specify the reliable pattern on the fileName / FilePath. Based on this we will select the strategy to extract / access e.g. by Regex, String Functions…
But we will wait first for your finalized and reliable requirement description

Hi @ppr

Sorry, initially i though if get a list having latest file names then check them with other DataTable based on this i planned to download file.

After seeing the query you provided it was relied on pattern. As i have to check with other data tables. so i felt it would be better if i have a query based on filename (i provide) it should check provide me the latest file from list.

As we dont have the control over the filenames. at present the file name patterns that we see are

Hi @ppr

I did managed and write a regex. If use static filename it is working as expected but when i make it dynamic file name it throws error “Enumerable.WhereListIterator { ! … }”

I verified the input value provided is correct. could help us

File_List.Where(Function(m) System.Text.RegularExpressions.Regex.IsMatch(m.Name,MyFileName+“_[0-9]{8}.(txt|xlsx).zip”))

please do one favor for me

set a breakpoint after you have retrieved the fileInfoObjects
debug an get paused
Open immediate panel
write in the statement: File_List.Select(Function (x) x.Name).toList

Thanks for support

Hi @ppr

I did. All file names were showing

:slight_smile: the intention of the request was to share output with me

maybe we can use following for the group key:

and this for the date

Import System.Text.RegularExpressions to the namespaces (down next to variable panel . imports)
then we can use short version: Regex.Match, Regex.isMatch

Give a try on:

(From x in yourIEFtpInfoVar
Let gn = Regex.Match(x.Name, "^.*?(?=\d{8})").toString
Group x by k=gn into grp=Group
Let lst = grp.OrderBy(Function (x) DateTime.ParseExact(Regex.Match(x.Name,"\d{8}").toString,"yyyyMMdd", CultureInfo.InvariantCulture)).Last()
Select f=lst).toList

HI @ppr


check filenames without date info

Yup its working. 1 file has incorrect dateformat.

1 last doubt regarding my expression the below is working

File_List.Where(Function(m) System.Text.RegularExpressions.Regex.IsMatch(m.Name,“KSF007_Skuldsanering”+“_[0-9]{8}.(txt|xlsx).zip”))

this is not working – filename1 is KSF007_Skuldsanering

File_List.Where(Function(m) System.Text.RegularExpressions.Regex.IsMatch(m.Name,filename1+“_[0-9]{8}.(txt|xlsx).zip”))

only change is instead of static name i put a variable to make it dynamic. Did i made anything wrong ?