使用Delphi递归删除所有文件和文件夹 [英] Delete all files and folders recursively using Delphi

查看:241
本文介绍了使用Delphi递归删除所有文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归删除一个文件夹及其所有的子文件夹,但是它根本不工作,所以有人可以检查代码,告诉我我在这里做错什么?

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 here?

我在Windows XP下通过D7运行此代码

I am running this code through D7 under Windows XP

if FindFirst (FolderPath + '\*', faAnyFile, f) = 0 then
      try             
         repeat

            if (f.Attr and faDirectory) <> 0 then
              begin
                    if (f.Name <> '.') and (f.Name <> '..') then
                      begin                            
                        RemoveDir(FolderPath +'\'+ f.Name);
                      end
                    else
                      begin
                        //Call function recursively...
                        ClearFolder(FolderPath +'\'+ f.Name, mask, recursive);
                      end;
              end;

         until (FindNext (f) <> 0);
      finally
        SysUtils.FindClose (f)
      end;
end;


推荐答案

而不是自己做所有这些辛勤工作, d只需使用 SHFileOperation

Rather than do all this hard work yourself, I'd just use SHFileOperation:

uses
  ShellAPI;

procedure DeleteDirectory(const DirName: string);
var
  FileOp: TSHFileOpStruct;
begin
  FillChar(FileOp, SizeOf(FileOp), 0);
  FileOp.wFunc := FO_DELETE;
  FileOp.pFrom := PChar(DirName+#0);//double zero-terminated
  FileOp.fFlags := FOF_SILENT or FOF_NOERRORUI or FOF_NOCONFIRMATION;
  SHFileOperation(FileOp);
end;

为什么值得,您的代码的问题是它不会调用 DeleteFile 。所以目录永远不会被清空,对 RemoveDir 的调用失败等等。您的代码中缺少错误检查并不真正有帮助,但是添加代码以删除文件会使该代码处于半体态。您还需要注意递归。您必须确保所有的孩子都被删除,然后删除父容器。这需要一定的技能才能正确。基本的方法是这样的:

For what it is worth, the problem with your code is that it doesn't ever call DeleteFile. And so the directories are never getting emptied, the calls to RemoveDir fail and so on. The lack of error checking in your code doesn't really help, but adding code to delete files would get that code in half-decent shape. You also need to take care with the recursion. You must make sure that all the children are deleted first, and then the parent container. That takes a certain degree of skill to get right. The basic approach is like this:

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

为了清楚起见,我省略了错误检查,但您应该检查 DeleteFile RemoveDir

I've omitted error checking for the sake of clarity, but you should check the return values of DeleteFile and RemoveDir.

这篇关于使用Delphi递归删除所有文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆