Files with Sort

ta5
Member Posts: 1,164
Hi experts
I try to loop on all Files in given directory using .NET.
It works fine, but I want to order by file date.
The code so far:
Found some tips to sort the array but I think there must be more straight forward solution, for example here
https://stackoverflow.com/questions/52842/sorting-directory-getfiles
but I was not able to adapt it to C/AL
Any ideas?
Thanks in advance
Thomas
I try to loop on all Files in given directory using .NET.
It works fine, but I want to order by file date.
The code so far:
// ServerDirectoryInfo DotNet System.IO.DirectoryInfo.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' // ServerFiles DotNet System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' // ServerFile DotNet System.IO.FileInfo.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' // iCount Integer // NoOfFiles Integer ServerDirectoryInfo := ServerDirectoryInfo.DirectoryInfo('c:\temp\test'); ServerFiles := ServerDirectoryInfo.GetFiles(); NoOfFiles := ServerFiles.Length(); FOR iCount := 0 TO NoOfFiles - 1 DO BEGIN ServerFile := ServerFiles.GetValue(iCount); MESSAGE('File No. %1\File Name %2',iCount + 1,ServerFile.Name); END;
Found some tips to sort the array but I think there must be more straight forward solution, for example here
https://stackoverflow.com/questions/52842/sorting-directory-getfiles
but I was not able to adapt it to C/AL

Any ideas?
Thanks in advance
Thomas
0
Best Answers
-
Hi Aluan
Just tried it on NAV2016.
Unfortunately i get this error:
"A call to System.Reflection.RuntimeMethodInfo.Invoke failed with this message: Parameter count mismatch."
Line: MethodInfo.Invoke(TYPE, Parameters);
Btw: My .NET experience is a bit limited, I don't fully understand that block anyway (especially the last line which is also the one throwing the error)TYPE := GETDOTNETTYPE(arr); MethodInfo := TYPE.GetMethods().GetValue(80); Parameters := Parameters.CreateInstance(GETDOTNETTYPE(Object),2); Parameters.SetValue(arrKey,0); Parameters.SetValue(arr,1); MethodInfo.Invoke(TYPE, Parameters);
Any ideas?
Thanks
Thomas
0 -
Sorry for the late response.
So u get the error as well with the report?
Well i am not sure, why u get this error. Have u tried the solution without .NET?5
Answers
-
Nobody?
Maybe may question was not written fully understandable? Sorry for my bad english, if somebody has experience with the topic above, but did not understand where my problem is: Don't hesitate to ask for clarification. On the other side, maybe it's kind of phantom problem? Thank you in advance.
Thomas0 -
Hey ta5,
does that help?
https://stackoverflow.com/questions/34439416/list-of-files-from-a-directory-ordered-by-date
Your links shows a solution with LINQ. I am not sure if u can even use it in Dynamics NAV.0 -
HI Aluan
Cool, did not see that. I'll give it a try!
Thx for your help
Thomas0 -
Hi Aluan
Just tried it on NAV2016.
Unfortunately i get this error:
"A call to System.Reflection.RuntimeMethodInfo.Invoke failed with this message: Parameter count mismatch."
Line: MethodInfo.Invoke(TYPE, Parameters);
Btw: My .NET experience is a bit limited, I don't fully understand that block anyway (especially the last line which is also the one throwing the error)TYPE := GETDOTNETTYPE(arr); MethodInfo := TYPE.GetMethods().GetValue(80); Parameters := Parameters.CreateInstance(GETDOTNETTYPE(Object),2); Parameters.SetValue(arrKey,0); Parameters.SetValue(arr,1); MethodInfo.Invoke(TYPE, Parameters);
Any ideas?
Thanks
Thomas
0 -
Hi Thomas,
i just copied the example, and it did work for me.
Anyway, you need to do it with .NET? What are you trying to reach - for what do you need the files sorted by date?0 -
Hi Aluan
Thanks.
Strange, what NAV Version and what .NET Version did you use?
Can you copy your version of the code here, otherwise by PM?
They have to be ordered by date, because it's a kind of file interface. Oldest files must be processed first because some data will be re-delivered in newer files. Newest data is always the one we need.
I could change to something different but it's our design pattern here, proved in tons of cases and very flexible concerning server side vs. client side file system manipulations...
Btw: For the moment we have a workaround, file names are like "file_yyyymmdd.txt", so standard sorting on name does the trick too.
Thanks again
Thomas0 -
Hi Thomas,
NAV 2016 & .NET 4.0. I created a report and exported it to a ".txt" file.
https://pastebin.com/Us9KRqDL
That did work for me.0 -
Hi Aluan
Thanks a lot. Strange, I've tested on different environments, even on a NAV 2018 (11.0.19846.0), with .NET Framework 4.5 installed. Everywhere the same error.0 -
Sorry for the late response.
So u get the error as well with the report?
Well i am not sure, why u get this error. Have u tried the solution without .NET?5 -
No problem. Yes, same error.
As mentioned above, I have a workaround, still using .NET, but sorting on the name. The name has the date part as a postfix. Sort is almost as good as on the change date itself.
I think I'll keep it like that, because it is not a super critical process.
Thanks again four your help.1
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions