delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


Files/DirectoriesGo to Abdulaziz Jasser's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
11/01/2000
Search Keys:
delphi delphi3000 article borland vcl code-snippet Files Directories API
Times Scored:
10
Visits:
9047
Uploader: Abdulaziz Jasser
Company: Saudi Telecommunications Compa
Reference: N/A
 
Question/Problem/Abstract:
How can delete a directory with its files and subdirectories?
Answer:



In Delphi there is a function called RemoveDir that delete empty directories.  But what about directories that has files and sub-sub... directories?

For this reason I implemented my own function to act like the normal RemoveDir function but with extra functionality to delete any files or subdirectories in side that directory.

Function MyRemoveDir(sDir : String) : Boolean;
var
         iIndex    : Integer;
         SearchRec : TSearchRec;
         sFileName : String;
begin
         Result := False;
         sDir   := sDir + '\*.*';
         iIndex := FindFirst(sDir, faAnyFile, SearchRec);

         while iIndex = 0 do begin
               sFileName := ExtractFileDir(sDir)+'\'+SearchRec.Name;
               if SearchRec.Attr = faDirectory then begin
                  if (SearchRec.Name <> '' )  and
                     (SearchRec.Name <> '.')  and
                     (SearchRec.Name <> '..') then
                      MyRemoveDir(sFileName);
               end else begin
                  if SearchRec.Attr <> faArchive then
                     FileSetAttr(sFileName, faArchive);
                  if NOT DeleteFile(sFileName) then
                     ShowMessage('Could NOT delete ' + sFileName);
               end;
               iIndex := FindNext(SearchRec);
         end;

         FindClose(SearchRec);

         RemoveDir(ExtractFileDir(sDir));
         Result := True;
end;

Example:

if NOT MyRemoveDir('D:\myDir') then
   ShowMessage('Can NOT delete dir');


Note:
(1) Any system/hidden/read-only files will be deleted.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Have a look at the API-funktion
    CRA (Nov 1 2000 7:03AM)

SHfileOperation

uses.....,ShellAPI

// action could be FO_MOVE,FO_DELETE,FO_COPY,FO_RENAME
procedure FileSystemAction(action:longint;fromDir,toDir:string);
var SHFileOp:TSHFileOpStruct;
begin
SHFileOp.wnd   :=Form1.handle;
SHFileOp.wFunc :=action;
SHFileOp.pFrom :=Pchar(fromDir  +#0+#0);
SHFileOp.pTo   :=Pchar(toDir    +#0+#0);
SHFileOp.fFlags:=FOF_SILENT or FOF_NOCONFIRMATION;
SHFileOp.fAnyOperationsAborted:=false;
SHFileOp.hNameMappings        :=NIL;
SHFileOp.lpszProgressTitle    :=NIL;
SHFileOperation(SHFileOp);
end;
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)