Delphi, delete folder with content

后端 未结 5 993
半阙折子戏
半阙折子戏 2020-12-14 23:07

when I have subfolder in folder - this code isn\'t delete folders... Is there any error?

procedure TForm.Remove(Dir: String);
var
  Result: TSearchRec; Found         


        
相关标签:
5条回答
  • 2020-12-14 23:33

    The last time I needed to delete a folder with content I used the JCL:

    uses JclFileUtils;
    
    DeleteDirectory(DirToDelete, True);
    

    The last parameter tells whether the files should go to the recycle bin or not, which is a nice bonus.

    0 讨论(0)
  • 2020-12-14 23:42
    uses DSiWin32;
    
    DSiDeleteTree(folderName, false);
    

    DSiWin32 is open source project relased with "use as you wish" license.

    0 讨论(0)
  • 2020-12-14 23:44

    If I were you, I'd just tell the operating system to delete the folder with all of its content. Do so by writing (uses ShellAPI)

    var
      ShOp: TSHFileOpStruct;
    begin
      ShOp.Wnd := Self.Handle;
      ShOp.wFunc := FO_DELETE;
      ShOp.pFrom := PChar('C:\Users\Andreas Rejbrand\Desktop\Test\'#0);
      ShOp.pTo := nil;
      ShOp.fFlags := FOF_NO_UI;
      SHFileOperation(ShOp);
    

    [If you do

      ShOp.fFlags := 0;
    

    instead, you get a nice confirmation dialog. If you do

    ShOp.fFlags := FOF_NOCONFIRMATION;
    

    you don't get the confirmation dialogue, but you do get a progress bar if the operation is lengthy. Finally, if you add the FOF_ALLOWUNDO flag, you move the directory to the Waste Bin instead of permanently deleting it.

    ShOp.fFlags := FOF_ALLOWUNDO;
    

    Of course, you can combine flags as you like:

    ShOp.fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO;
    

    will not show any confirmation (but a progress dialog because you don't specify FOF_NO_UI) and the directory will be moved to the waste bin and not permanently deleted.]

    0 讨论(0)
  • 2020-12-14 23:52

    To address the original problem - try this:

    procedure TForm.Remove(const Dir: String);
    var
      sDir: String;
      Rec: TSearchRec;
    begin
      sDir := IncludeTrailingPathDelimiter(Dir);
      if FindFirst(sDir + '*.*', faAnyFile, Rec) = 0 then
      try
        repeat
          if (Rec.Attr and faDirectory) = faDirectory then
          begin
            if (Rec.Name <> '.') and (Rec.Name <> '..') then
              Remove(sDir + Rec.Name);
          end else
          begin
            DeleteFile(sDir + Rec.Name);
          end;
        until FindNext(Rec) <> 0;
      finally
        FindClose(Rec);
      end;
      RemoveDir(sDir);
    end; 
    
    0 讨论(0)
  • 2020-12-14 23:53

    The simplest thing to do is to call TDirectory.Delete(Dir, True).

    TDirectory is found in IOUtils which is quite a recent RTL addition.

    The True flag is passed to the Recursive parameter which means that the contents of the directories are empied before the directory is removed, an essential part of deleting directories.


    In a comment you tell us that you use Delphi 7 and so this cannot be used.

    Your code looks mostly fine. However, you don't mean:

    (Result.Attr and faAnyFile <> faDirectory)
    

    I think you mean:

    (Result.Attr and faDirectory <> faDirectory)
    

    I would probably write it as follows:

    procedure TMyForm.Remove(const Dir: string);
    var
      Result: TSearchRec;
    begin
      if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
      begin
        Try
          repeat
            if (Result.Attr and faDirectory) = faDirectory then
            begin
              if (Result.Name <> '.') and (Result.Name <> '..') then
                Remove(Dir + '\' + Result.Name)
            end
            else if not DeleteFile(Dir + '\' + Result.Name) then
              RaiseLastOSError;
          until FindNext(Result) <> 0;
        Finally
          FindClose(Result);
        End;
      end;
      if not RemoveDir(Dir) then
        RaiseLastOSError;
    end;
    
    0 讨论(0)
提交回复
热议问题