Files with Sort

ta5ta5 Member Posts: 1,164
edited 2018-09-06 in NAV Three Tier
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:
// 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

Best Answers

  • ta5ta5 Member Posts: 1,164
    edited 2018-09-14 Answer ✓
    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

Answers

  • ta5ta5 Member Posts: 1,164
    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.
    Thomas
  • AluanAluan Member Posts: 158
    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.
  • ta5ta5 Member Posts: 1,164
    HI Aluan
    Cool, did not see that. I'll give it a try!
    Thx for your help
    Thomas
  • ta5ta5 Member Posts: 1,164
    edited 2018-09-14 Answer ✓
    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

  • AluanAluan Member Posts: 158
    edited 2018-09-14
    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?
  • ta5ta5 Member Posts: 1,164
    edited 2018-09-14
    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
    Thomas
  • AluanAluan Member Posts: 158
    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.
  • ta5ta5 Member Posts: 1,164
    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. :/
  • ta5ta5 Member Posts: 1,164
    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.
Sign In or Register to comment.