Board index » delphi » Deleting Files in directory and Directory itself

Deleting Files in directory and Directory itself

I am trying to replicate the functionality of DOS's DelTree. I would like to
be able to delete a directory.  I can delete all the files in the Directory
using a tsearchrec and delete the files.  How do I delete the directory or
is there an easier way of removing the directory and any subdir's under it.

TIA

DAN

 

Re:Deleting Files in directory and Directory itself


I think there is no other way. As you wrote, use TSearchRec and then
RemoveDirectory.
You could do it like this (from Borland FAQ), but look out for
subdirectories!

procedure TForm1.Button1Click(Sender: TObject);
var
  DirInfo: TSearchRec;
  r : Integer;
begin
  r := FindFirst('C:\Download\Test\*.*', FaAnyfile, DirInfo);
  while r = 0 do  begin
    if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
        (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
      if DeleteFile(pChar('C:\Download\test\' + DirInfo.Name))
         = false then
       ShowMessage('Unable to delete : C:\Download\test\' +
                   DirInfo.Name);
    r := FindNext(DirInfo);
  end;
  SysUtils.FindClose(DirInfo);
  if RemoveDirectory('C:\Download\Test') = false then
    ShowMessage('Unable to delete direcotry : C:\Download\test');
end;

Hope that helps,
Matt

Re:Deleting Files in directory and Directory itself


Use function ShFileOperation

Quote
Dan <azt...@aztechsoftware.com> wrote in message

news:84geh5$mde6@forums.borland.com...
Quote
> I am trying to replicate the functionality of DOS's DelTree. I would like
to
> be able to delete a directory.  I can delete all the files in the
Directory
> using a tsearchrec and delete the files.  How do I delete the directory or
> is there an easier way of removing the directory and any subdir's under
it.

> TIA

> DAN

Other Threads