Delphi,删除包含内容的文件夹

发布于 2024-11-02 03:51:11 字数 609 浏览 1 评论 0原文

当我的文件夹中有子文件夹时 - 此代码不会删除文件夹...有任何错误吗?

procedure TForm.Remove(Dir: String);
var
  Result: TSearchRec; Found: Boolean;
begin
  Found := False;
  if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
    while not Found do begin
      if (Result.Attr and faDirectory = faDirectory) AND (Result.Name <> '.') AND (Result.Name <> '..') then Remove(Dir + '\' + Result.Name)
      else if (Result.Attr and faAnyFile <> faDirectory) then DeleteFile(Dir + '\' + Result.Name);
      Found := FindNext(Result) <> 0;
    end;
  FindClose(Result); RemoveDir(Dir);
end;

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: Boolean;
begin
  Found := False;
  if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
    while not Found do begin
      if (Result.Attr and faDirectory = faDirectory) AND (Result.Name <> '.') AND (Result.Name <> '..') then Remove(Dir + '\' + Result.Name)
      else if (Result.Attr and faAnyFile <> faDirectory) then DeleteFile(Dir + '\' + Result.Name);
      Found := FindNext(Result) <> 0;
    end;
  FindClose(Result); RemoveDir(Dir);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

戴着白色围巾的女孩 2024-11-09 03:51:11

最简单的方法是调用 TDirectory.Delete(Dir, True)

TDirectoryIOUtils 这是最近添加的 RTL。

True 标志传递给 Recursive 参数,这意味着在删除目录之前清空目录的内容,这是删除目录的重要部分。


在评论中,您告诉我们您使用的是 Delphi 7,因此无法使用它。

你的代码看起来大部分都很好。但是,您的意思并不是:

(Result.Attr and faAnyFile <> faDirectory)

我认为您的意思是:

(Result.Attr and faDirectory <> faDirectory)

我可能会写如下:

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;

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;
吐个泡泡 2024-11-09 03:51:11

如果我是您,我只会告诉操作系统删除该文件夹及其所有内容。通过编写 (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);

[如果你

  ShOp.fFlags := 0;

这样做,你会得到一个很好的确认对话框。如果这样做,

ShOp.fFlags := FOF_NOCONFIRMATION;

您不会看到确认对话框,但如果操作很长,您会看到一个进度条。最后,如果添加 FOF_ALLOWUNDO 标志,则会将该目录移动到垃圾箱,而不是永久删除它。

ShOp.fFlags := FOF_ALLOWUNDO;

当然,您可以根据需要组合标志:

ShOp.fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO;

不会显示任何确认(而是显示进度对话框,因为您没有指定FOF_NO_UI)并且目录将被移动到垃圾箱并且不会永久删除.]

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.]

傲鸠 2024-11-09 03:51:11

上次我需要删除包含内容的文件夹时,我使用了 JCL

uses JclFileUtils;

DeleteDirectory(DirToDelete, True);

最后一个参数告诉我们文件是否应该进入回收站,这是一个很好的好处。

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.

别靠近我心 2024-11-09 03:51:11

要解决最初的问题 - 尝试以下操作:

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; 

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; 
音盲 2024-11-09 03:51:11
uses DSiWin32;

DSiDeleteTree(folderName, false);

DSiWin32 是一个开源项目,具有“随心所欲使用”许可证。

uses DSiWin32;

DSiDeleteTree(folderName, false);

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

独自唱情﹋歌 2024-11-09 03:51:11

此过程适用于不同的格式:

procedure Remove(const FileName: string);
var
  Path: string;
  SearchRec: TSearchRec;
  procedure RemoveDirectory(const Dir: string);
  var
    SearchRec: TSearchRec;
  begin
    if FindFirst(Dir + '\*', faAnyFile, SearchRec) = 0 then
    begin
      try
        repeat
          if (SearchRec.Attr and faDirectory) = faDirectory then
          begin
            if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
              RemoveDirectory(Dir + '\' + SearchRec.Name)
          end
          else
            System.SysUtils.DeleteFile(Dir + '\' + SearchRec.Name);
        until FindNext(SearchRec) <> 0;
      finally
        System.SysUtils.FindClose(SearchRec);
      end;
    end;
    RemoveDir(Dir);
  end;

begin
  if DirectoryExists(FileName) then
    RemoveDirectory(FileName)
  else if FindFirst(FileName, faAnyFile, SearchRec) = 0 then
  begin
    repeat
      if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
        Continue;

      Path := ExtractFilePath(FileName) + '\' + SearchRec.Name;
      if DirectoryExists(Path) then
        RemoveDirectory(Path)
      else
        System.SysUtils.DeleteFile(Path);

    until FindNext(SearchRec) <> 0;

    System.SysUtils.FindClose(SearchRec);
  end;
end;
  • D:\myFolder\ #删除目录
  • D:\myFolder\* #删除所有文件和目录目录
  • D:\myFolder\*.txt #删除文本文件
  • D:\myFolder\*lib*.dll #删除特殊dll文件
  • D:\myFolder\readme.txt #删除文件

This procedure works for different formats:

procedure Remove(const FileName: string);
var
  Path: string;
  SearchRec: TSearchRec;
  procedure RemoveDirectory(const Dir: string);
  var
    SearchRec: TSearchRec;
  begin
    if FindFirst(Dir + '\*', faAnyFile, SearchRec) = 0 then
    begin
      try
        repeat
          if (SearchRec.Attr and faDirectory) = faDirectory then
          begin
            if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
              RemoveDirectory(Dir + '\' + SearchRec.Name)
          end
          else
            System.SysUtils.DeleteFile(Dir + '\' + SearchRec.Name);
        until FindNext(SearchRec) <> 0;
      finally
        System.SysUtils.FindClose(SearchRec);
      end;
    end;
    RemoveDir(Dir);
  end;

begin
  if DirectoryExists(FileName) then
    RemoveDirectory(FileName)
  else if FindFirst(FileName, faAnyFile, SearchRec) = 0 then
  begin
    repeat
      if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
        Continue;

      Path := ExtractFilePath(FileName) + '\' + SearchRec.Name;
      if DirectoryExists(Path) then
        RemoveDirectory(Path)
      else
        System.SysUtils.DeleteFile(Path);

    until FindNext(SearchRec) <> 0;

    System.SysUtils.FindClose(SearchRec);
  end;
end;
  • D:\myFolder\ #remove directory
  • D:\myFolder\* #remove all files & directories
  • D:\myFolder\*.txt #remove text files
  • D:\myFolder\*lib*.dll #remove special dll files
  • D:\myFolder\readme.txt #remove file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文