Question : Delphi get last modified file in folder

Good day

I need to find a file that has been last modified in a folder. I dont know the filename, I only know the directory and the extension of the file.

Does anyone know how to return the name latest file in a directory if I know the extension.

Regards

Answer : Delphi get last modified file in folder

This will do. It does not search in sub directory
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
function FindLastWrittenFile(Dir,Search:String):String;
Var
 R:TSearchRec;
 LastWrite,LastWriteAllFiles:TDateTime;
begin
 Dir:=IncludeTrailingBackslash(Dir);
 LastWriteAllFiles:=0;
 Result:='';
 if FindFirst(Dir+Search,faAnyFile-faDirectory,R)=0 then
  repeat
   LastWrite := FileTimeToDTime(R.FindData.ftLastWriteTime);;
   if LastWrite>LastWriteAllFiles Then
    begin
     LastWriteAllFiles:=LastWrite;
     Result:=R.Name;
    end;
  until FindNext(R)<>0;
 FindClose(R); 
end;
Random Solutions  
 
programming4us programming4us