Board index » delphi » Want to delete directory and its contents

Want to delete directory and its contents

I want to delete a directory that will not be used again in the future.
I am creating new directories that roll-over the current years data to
the next. This is by request of the client. They need to work in the
current year and next. I need a way to delete the existing files in a
directory and then delete the directory itself.

I tried the following:

var
  SearchRec: TSearchRec;

      CurDir := EditDeleteDirectory.Text;
      if FindFirst(CurDir+'\*.*', faAnyFile, SearchRec) = 0 then
        DeleteFile(SearchRec.Name);
      repeat
        if FindNext(SearchRec) = 0 then
          DeleteFile(SearchRec.Name);
      until FindNext(SearchRec) <> 0;
      FindClose(SearchRec);
      RemoveDir(CurDir);

This would find maybe two or three files and then FindNext returned 18
indicating it could find no more files. It did not even delete the ones
it did find.

Am I missing something here? Is there a better way to do this?

dick Mathews
issp...@earthlink.net

 

Re:Want to delete directory and its contents


Quote
>       repeat
>         if FindNext(SearchRec) = 0 then
>           DeleteFile(SearchRec.Name);
>       until FindNext(SearchRec) <> 0;

You are repeating the FindNext command twice each time through the loop.
Eliminate one of them.

Woody

Re:Want to delete directory and its contents


This is a small modification of your code

var
  SearchRec: TSearchRec;
  Code, Len: Integer;

      CurDir := EditDeleteDirectory.Text;
      Len := Length(CurDir);
      if (L = 0) or (CurDir[L] <> '\') then CurDir := CurDir + '\';
      Code := FindFirst(CurDir+'*.*', faAnyFile, SearchRec);
      while Code = 0 do begin
        DeleteFile(CurDir + SearchRec.Name);
        Code := FindNext(SearchRec);
      end;
      FindClose(SearchRec);
      RemoveDir(CurDir);

Re:Want to delete directory and its contents


Use ShFileOperation Api
it can delete subfolder  and files too without us having to code a lot !

 {*word*106} Mathews <issp...@earthlink.net> wrote in message news:37A4E24C.8A743EFA@earthlink.net...
  I want to delete a directory that will not be used again in the future. I am creating new directories that roll-over the current years data to the next. This is by request of the client. They need to work in the current year and next. I need a way to delete the existing files in a directory and then delete the directory itself.
  I tried the following:

  var
    SearchRec: TSearchRec;

        CurDir := EditDeleteDirectory.Text;
        if FindFirst(CurDir+'\*.*', faAnyFile, SearchRec) = 0 then
          DeleteFile(SearchRec.Name);
        repeat
          if FindNext(SearchRec) = 0 then
            DeleteFile(SearchRec.Name);
        until FindNext(SearchRec) <> 0;
        FindClose(SearchRec);
        RemoveDir(CurDir);

  This would find maybe two or three files and then FindNext returned 18 indicating it could find no more files. It did not even delete the ones it did find.

  Am I missing something here? Is there a better way to do this?

 {*word*106} Mathews
  issp...@earthlink.net

Re:Want to delete directory and its contents


Thanks for the help, it worked great.

dick

Quote
Alex_IA wrote:
> This is a small modification of your code

> var
>   SearchRec: TSearchRec;
>   Code, Len: Integer;

>       CurDir := EditDeleteDirectory.Text;
>       Len := Length(CurDir);
>       if (L = 0) or (CurDir[L] <> '\') then CurDir := CurDir + '\';
>       Code := FindFirst(CurDir+'*.*', faAnyFile, SearchRec);
>       while Code = 0 do begin
>         DeleteFile(CurDir + SearchRec.Name);
>         Code := FindNext(SearchRec);
>       end;
>       FindClose(SearchRec);
>       RemoveDir(CurDir);

Other Threads