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:
ABC_20210921
PQR_20210921
JKL_20210921
ABC_20210920
PQR_20210915
JKL_20210912
CSG_20210910
CYC_20210921
CSG_20210906
CYC_20210810

Expected output: for each file (ABC, PQR etc) i want latest file.
ABC_20210921
PQR_20210921
JKL_20210921
CSG_20210910
CYC_20210921

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

@sunilch

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

@sunilch
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

Philip_20210921.txt.zip
123-1_20210921.txt.zip
InD-1_567-54_20210921.txt.zip
123_20210921.txt.zip
agent-2_12_20210810.txt.zip
Philip_20210918.txt.zip
123-1_20210907.txt.zip

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:
grafik

and this for the date
grafik

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
2 Likes

HI @ppr

image

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 ?