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.
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
(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
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
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
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
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
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