Solved

D1:Recursiv deletion of directories and content

Posted on 1997-12-10
4
219 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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