19-11-2022, 10:07 AM
انظر في هذا الكود الذي يمكنه حذف ملفات المؤقتة يجب توقع ان بعض الملفات لايمكن حذفها
كود :
uses ShellAPI;
procedure RemoveTempFiles();
var
Rec: TSearchRec;
S,t,Dir:string;
FOS: ShellAPI.TSHFileOpStruct; // contains info about required file operation
Ph:array[byte] of Widechar;
begin
Ph:='';
Winapi.Windows.GetTempPath(256,PWideChar(@Ph));// temp path
Dir:=ExtractFileDir(ph);
S:= '';
if FindFirst(Dir + '\*', faAnyFile, Rec) = 0 then
try
repeat
if (Rec.Attr and faDirectory) = faDirectory then
begin
if (Rec.Name = '.') or (Rec.Name = '..') then
continue;
end;
t:=Dir + '\' + Rec.Name;
SetFileAttributes(PChar(t),Rec.Attr and not $3);
S:= S+t+#0;
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
if S <> '' then
begin
S:= S+#0;
FillChar(FOS, SizeOf(FOS), 0);
with FOS do
begin
wFunc := ShellAPI.FO_DELETE; // we're deleting
pFrom := PChar(S); // file list (#0#0 terminated)
fFlags := ShellAPI.FOF_ALLOWUNDO // with facility to undo op
or ShellAPI.FOF_NOCONFIRMATION // and we don't want any dialogs
or ShellAPI.FOF_SILENT;
ShellAPI.SHFileOperation(FOS)
end;
end;
end;
