Author: Holger Voigt
Sometimes its nessessary to know which File of an Folder was changed at the last.
Answer:
Sometimes its nessessary to know which File of an Folder was changed at the last. I
wrote this function below. It can give you the File with the oldesd content too, if
you set the parameter first := True. For the comparing I've used the API - Function
CompareFileTime. If you wants to know the oldes or youngest created file or the
last/first accessed file see the comments in the function.
1 2 function GetLastOrFirstChangedFileOfFolder(First: Boolean; Folder: string): string;
3 var4 Ft1, Ft2: TFileTime;
5 sr: TSearchrec;
6 what, Res: Integer;
7 begin8 ifnot DirectoryExists(Folder) then9 exit;
10 if Folder[Length(Folder)] <> '\' then11 Folder := Folder + '\';
12 if First then13 What := 1
14 else15 What := -1;
16 res := FindFirst(Folder + '*.*', faAnyFile, sr);
17 ft1 := sr.FindData.ftLastWriteTime;
18 //ft1 := sr.FindData.ftCreationTime; for the first/last created19 //ft1 := sr.FindData.ftLastAccessTime; for the first/last access20 Result := sr.Name;
21 while res = 0 do22 begin23 Ft2 := sr.FindData.ftLastWriteTime;
24 //ft1 := sr.FindData.ftCreationTime; for the first/last createt25 //ft1 := sr.FindData.ftLastAccessTime; for the first/last access26 if CompareFileTime(ft1, ft2) = What then27 begin28 ft1 := Ft2;
29 Result := sr.Name;
30 end;
31 res := FindNext(sr);
32 end;
33 FindClose(sr);
34 end;