First/Last changed File in Folder (Views: 29)
Problem/Question/Abstract: 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. function GetLastOrFirstChangedFileOfFolder(First: Boolean; Folder: string): string; var Ft1, Ft2: TFileTime; sr: TSearchrec; what, Res: Integer; begin if not DirectoryExists(Folder) then exit; if Folder[Length(Folder)] <> '\' then Folder := Folder + '\'; if First then What := 1 else What := -1; res := FindFirst(Folder + '*.*', faAnyFile, sr); ft1 := sr.FindData.ftLastWriteTime; //ft1 := sr.FindData.ftCreationTime; for the first/last created //ft1 := sr.FindData.ftLastAccessTime; for the first/last access Result := sr.Name; while res = 0 do begin Ft2 := sr.FindData.ftLastWriteTime; //ft1 := sr.FindData.ftCreationTime; for the first/last createt //ft1 := sr.FindData.ftLastAccessTime; for the first/last access if CompareFileTime(ft1, ft2) = What then begin ft1 := Ft2; Result := sr.Name; end; res := FindNext(sr); end; FindClose(sr); end; |