Delete all files and folders recursively using Delphi

前端 未结 2 656
清酒与你
清酒与你 2020-12-09 18:44

I am trying to delete a folder and all of its sub-folders recursively but it is not working at all, so can someone please check the code and tell me what I am doing wrong he

2条回答
  •  孤街浪徒
    2020-12-09 19:41

    procedure DeleteDir(const DirName: string);
    var
      Path: string;
      F: TSearchRec;
    
    begin
      Path:= DirName + '\*.*';
      if FindFirst(Path, faAnyFile, F) = 0 then begin
        try
          repeat
            if (F.Attr and faDirectory <> 0) then begin
              if (F.Name <> '.') and (F.Name <> '..') then begin
                DeleteDir(DirName + '\' + F.Name);
              end;
            end
            else
              DeleteFile(DirName + '\' + F.Name);
          until FindNext(F) <> 0;
        finally
          FindClose(F);
        end;
      end;
      RemoveDir(DirName);
    end;
    

提交回复
热议问题