Solved

D1:Recursiv deletion of directories and content

Posted on 1997-12-10
4
213 Views
Last Modified: 2010-04-04
How can I delete a directory with contents, including subdirectories in Delphi 1.0 (Windows 3.11) ?
0
Comment
Question by:zaibatzuka
  • 2
4 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1353287
Let me propose this :
 1. Look for a component which does recursif searching.
     (enough freeware components on the net)
 2. Delete every founded file or directory

Here is already a code for searchin the entire disk. Easely changed in searching through one directory.

unit Audit1;
interface
uses windos;

var
  dest:string;

procedure dorecurse(dir:string);

implementation
{$R *.DFM}
Procedure Process (dir:string; Searchrec:tsearchrec);
begin
   showmessage (Searchrec.name);
   case Searchrec.attr of
   $10:
      if (searchrec.name<>'.') and (searchrec.name<>'..') then
          begin
        dorecurse (dir+'\'+searchrec.name);
                writeln (dir);
          end;
   end;
end;

Procedure Dorecurse(dir:string);
var
   Searchrec:Tsearchrec;
   pc: array[0..79] of Char;
   
begin
   StrPCopy(pc, dir+'\*.*');
   FindFirst(pc, FaAnyfile, SearchRec);
   Process (dir,SearchRec);
   while FindNext(SearchRec)<>-18 do
   begin
      Process (dir,SearchRec);
   end;
end;

Procedure startsearch;
begin
   dorecurse (paramstr(1));
end;

begin
   startsearch;
end.

Have fun,
c.u. ZifNab;
0
 
LVL 1

Accepted Solution

by:
aluiken earned 100 total points
ID: 1353288
Hi there,

You should use findfirst and the findnext functions
then delete / process every file

You will need a procedure like this:

procedure DelTree(const RootDir  : String);
var
  SearchRec : TSearchRec;
begin
Try
    ChDir(RootDir);  {Path to the directory  given as parameter }
    FindFirst('*.*',faAnyFile,SearchRec);
    Erc := 0;
    while Erc = 0 do  begin
        if      ((SearchRec.Name <> '.' )
        and  (SearchRec.Name <> '..')) then begin
              if  (SearchRec.Attr and faDirectory>0) then begin
                    { Have found a directory, not a file.
                       Recusively call ouselves to delete its files }
                     DelTree(SearchRec.Name);
                     end
              else begin
                    {Found a file.  Delete it or whatever
                     you want to do here }
                     end;
         end;
          Erc := FindNext (SearchRec);
            otherwise Erc = negative DOS error }
            Application.ProcessMessages;
    end;
finally
      if Length(RootDir) > 3 then ChDir('..');
end;
end;

So thats it.

Regards, Arijan
0
 

Author Comment

by:zaibatzuka
ID: 1353289
OK. Note that the line above Application.ProcessMessages is missing a left braces. Indentations would have made it easier to follow the code.
Thanks.
0
 
LVL 1

Expert Comment

by:aluiken
ID: 1353290
hey a html memo is sumthing else then the borland delphi UI
=)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now