Title: Get folder/file names(in specified folder) and folder/file modified times
Question: Get the folder/file names in specified folder...
Answer:
uses
comctrls, ShellAPI, commctrl;
{...}
const
FL_FOLDER = 1;
FL_FILE = FL_FOLDER + 1;
{ File Date Types }
FD_CREATED = 1;
FD_ACCESSED = FD_CREATED + 1;
FD_MODIFIED = FD_ACCESSED + 1;
type
FileInfoPtr = ^FileInfoRec;
FileInfoRec = record
fName : string;
typ : byte;
Modified : TDateTime;
end;
function GetFileTimes(FileName : ShortString; typ : byte; var fDate : TDateTime) : Boolean;
var
fHandle : Integer;
fTimeC,
fTimeA,
fTimeM : TFileTime;
lTime : TFileTime;
sTime : TSystemTime;
begin { GetFileTimes }
fHandle := FileOpen(FileName, fmShareDenyNone);
fDate := 0.0;
result := (fHandle = 0);
if result
then begin GetFileTime(fHandle, @fTimeC, @fTimeA, @fTimeM);
FileClose(fHandle);
case typ of
FD_CREATED : FileTimeToLocalFileTime(fTimeC, lTime);
FD_ACCESSED : FileTimeToLocalFileTime(fTimeA, lTime);
FD_MODIFIED : FileTimeToLocalFileTime(fTimeM, lTime);
end;
if FileTimeToSystemTime(lTime, sTime)
then fDate := EncodeDate(sTime.wYear, sTime.wMonth, sTime.wDay) + EncodeTime(sTime.wHour, sTime.wMinute, sTime.wSecond, sTime.wMilliSeconds);
end;
end; { of GetFileTimes }
procedure GetDirList(dir : ShortString; List : TList; recursive : Boolean);
var
fsRec : TSearchRec;
fIPtr : FileInfoPtr;
begin { GetDirList }
if (dir = '')
then Exit;
if (dir[length(dir)] '\')
then dir := dir + '\';
try
if (FindFirst(dir + '*.*', faAnyFile, fsRec) = 0)
then repeat if (fsRec.Name '') AND (fsRec.Name '.') AND (fsRec.Name '..')
then begin fIPtr := AllocMem(SizeOf(FileInfoRec));
fIPtr^.fName := dir + fsRec.Name;
GetFileTimes(dir + fsRec.Name, FD_MODIFIED, fIPtr^.Modified);
if ((fsRec.Attr AND faDirectory) = faDirectory)
then begin fIPtr^.typ := FL_FOLDER;
List.Insert(0, fIPtr);
if recursive
then GetDirList(dir + fsRec.Name, List, TRUE);
end
else begin fIPtr^.typ := FL_FILE;
List.Add(fIPtr);
end;
end;
until (FindNext(fsRec) 0);
FindClose(fsRec);
except
end;
end; { of GetDirList }