delphi4arab منتديات دلفي للعرب

نسخة كاملة : Temp و %Temp%
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
مرحبا بالجميع
اود الإستفسار عن كيفية تفريغ مجلدي Temp و %Temp%  برمجيا باستخدام الدلفي
انظر في هذا الكود الذي يمكنه حذف ملفات المؤقتة  يجب توقع ان بعض الملفات لايمكن حذفها

كود :
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;
يوجد عدة طرق للتعامل مع الملفات و المجلدات ، سواءا من دلفي مباشرة أو مكونات خارجية

* توجد مكونات خارجية نذكر منها JVCL والتي تتكون من المكتبة المرئية JEDI Visual Component Library و مكتبة الاكواد JEDI Code Library والتي تحتوي
على مكتبة التعامل مع المجلدات و الملفات و التي نصرح بها JCLFileUtils و التي تحوي على قائمة من الاجراءات

نذكر منها
كود :
DeleteDirectory CopyDirectory MoveDirectory DelTree DelTreeEx DiskInDrive DirectoryExists FileCreateTemp FileBackup FileCopy FileDateTime FileDelete FileExists FileMove FileRestore GetBackupFileName IsBackupFileName FileGetDisplayName FileGetGroupName FileGetOwnerName FileGetSize FileGetTempName FileGetTypeName FindUnusedFileName ForceDirectories GetDirectorySize GetDriveTypeStr GetFileAgeCoherence GetFileAttributeList GetFileAttributeListEx GetFileInformation GetFileLastWrite GetFileLastAccess GetFileCreation

مثال للحذف محتوى المجلد %temp% و الذي نختصره بالدلفي بالامر TPath.GetTempPath

نستعمل الامر DeleteDirectory  ، بالنسبة الى True وهي نقل الملفات الى سلة المحذوفات و العكس حذف مباشر

كود :
DeleteDirectory (TPath.GetTempPath, false);

رابط تحميل المكتبتين jvcl اختصار إلى JEDI Visual Component Library و jcl مكتبة الاكواد JEDI Code Library

كود :
https://github.com/project-jedi



أو برمجيا

كود :
procedure RemoveTemp(const Dir: String); var   sDir: String;   Rec: TSearchRec; begin   sDir := IncludeTrailingPathDelimiter(Dir);   if FindFirst(sDir + '*.*', faAnyFile, Rec) = 0 then   try     repeat       if (Rec.Attr and faDirectory) = faDirectory then       begin         if (Rec.Name <> '.') and (Rec.Name <> '..') then           RemoveTemp(sDir + Rec.Name);       end else       begin         DeleteFile(sDir + Rec.Name);       end;     until FindNext(Rec) <> 0;   finally     FindClose(Rec);   end;   RemoveDir(sDir); end;


تنفيذ

كود :
RemoveTemp (TPath.GetTempPath);

ملاحظة :

توجد ملفات تكون متصلة ببرامج أو النظام لا يمكن حذفها الا عن طريق غلق البرنامج المتصلة به
بارك الله في الجميع