azatoth
azatoth

Reputation: 705

List of files from a directory ordered by date

How can I get a list of files from directory ordered by date in NAV? I can't use the File virtual table because there is no key for the date. I've tried with DotNet but implementing the IComparer interface is far too complicated for me.

Any ideas?

Cheers

Upvotes: 0

Views: 3698

Answers (2)

Mak Sim
Mak Sim

Reputation: 2314

And what is exactly wrong with the key of File table? As far as I can see both Date and Time are sortable. Tested in Nav 2015. Sorting virtual File table

Anyway, if you really want to do it with .net here is example. Base table is File. Page property SourceTableTemporary = Yes. After you get all file info to arrey you can use it and sort it the way you like. Or you can fill thetemporary file table with values from array and use setcurrentkey LOL

di  DotNet  System.IO.DirectoryInfo.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'   
fi  DotNet  System.IO.FileSystemInfo.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
arr DotNet  System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
i   Integer     

di := di.DirectoryInfo('c:\Temp\Tmp');
//arr.CreateInstance(GETDOTNETTYPE(fi),1); //not needed
arr := di.GetFileSystemInfos();
for i := 0 to arr.Length-1 do
 begin
  fi := arr.GetValue(i);
  Name := fi.Name;
  evaluate(Date, format(fi.LastWriteTime,8,1));
  insert;
 end;

Reading file list

Or you could even sort file list with .Net

di  DotNet  System.IO.DirectoryInfo.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'   
fi  DotNet  System.IO.FileSystemInfo.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
arr DotNet  System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
arrKey  DotNet  System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
i   Integer     
TYPE    DotNet  System.Type.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'   
MethodInfo  DotNet  System.Reflection.MethodInfo.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
Parameters  DotNet  System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
Object  DotNet  System.Object.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

di := di.DirectoryInfo('c:\Temp\Tmp');
arr := di.GetFileSystemInfos();
fi := arr.GetValue(0);
arrKey := arrKey.CreateInstance(GETDOTNETTYPE(fi.LastWriteTime), arr.Length);
FOR i := 0 TO arr.Length-1 DO
 BEGIN
  fi := arr.GetValue(i);
  arrKey.SetValue(fi.LastWriteTime, i);
 END;

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);

FOR i := 0 TO arr.Length-1 DO
 BEGIN
  fi := arr.GetValue(i);
  MESSAGE(FORMAT(fi.LastWriteTime()));
 END;

Upvotes: 0

Jan Hoek
Jan Hoek

Reputation: 712

As a low-tech solution (i.e. not requiring any external components), how about creating a temporary File record variable in which you buffer the file records, populating the Path field with a sortable string representation of the file's Date and Time fields combined into a DateTime? [Please ignore that PAGE.RUN - that was there just for debugging purposes.]

Code

On my machine, the first call to ShowFileOrder displays this:

Sorted by name

The second call shows this:

enter image description here

And this is what the files look like in Windows Explorer:

enter image description here

I hope this helps! :)

Upvotes: 1

Related Questions