删除具有非空子目录和文件的目录 [英] Delete Directory with non empty subdirectory and files

查看:170
本文介绍了删除具有非空子目录和文件的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除一个包含一些文件和一个非空子目录的目录。

我尝试了 SHFileOperation函数 。它在 Windows中,存在一些兼容性问题7

然后我尝试了 IFileOperation接口 。但是与 Windows XP 不兼容。
然后我尝试了 所建议的以下代码> David Heffernan

How to delete one directory having some files and some non empty sub directory.
I have tried SHFileOperation Function. It has some compatibility issue in Windows 7.
Then I have tried IFileOperation Interface. But it is not compatible in Windows XP. Then I have tried the following codes as suggested by David Heffernan :

procedure TMainForm.BitBtn01Click(Sender: TObject);
var
  FileAndDirectoryExist: TSearchRec;
  ResourceSavingPath : string;
begin
  ResourceSavingPath := (GetWinDir) + 'Web\Wallpaper\';
  if FindFirst(ResourceSavingPath + '\*', faAnyFile, FileAndDirectoryExist) = 0 then
  try
    repeat
      if (FileAndDirectoryExist.Name <> '.') and (FileAndDirectoryExist.Name <> '..') then
        if (FileAndDirectoryExist.Attr and faDirectory) <> 0 then
          //it's a directory, empty it
          ClearFolder(ResourceSavingPath +'\' + FileAndDirectoryExist.Name, mask, recursive)
        else
          //it's a file, delete it
          DeleteFile(ResourceSavingPath + '\' + FileAndDirectoryExist.Name);
    until FindNext(FileAndDirectoryExist) <> 0;
    //now that this directory is empty, we can delete it
    RemoveDir(ResourceSavingPath);
  finally
    FindClose(FileAndDirectoryExist);
  end;
end;

但是在 ClearFolder 上未编译为未声明标识符的提及错误, 掩码递归。我的要求是如果WALLPAPER文件夹下存在任何子文件夹,它将被删除。同一子文件夹可以包含任意数量的非空子文件夹或文件。

But it does not get compiled mentioning error as Undeclared Identifier at ClearFolder, mask and recursive. My requirement is to that "If any sub folder exist under WALLPAPER folder it will be deleted". The same sub folder may contain any number of non empty sub folder or files.

推荐答案

对于初学者来说, SHFileOperation 在Windows 7或Windows 8上没有兼容性问题。是,现在建议您改用 IFileOperation 。但是,如果要支持XP等较旧的操作系统,则可以并且应该只调用 SHFileOperation 。它有效,并且将继续起作用。最好在Windows 7和Windows 8上使用它,如果从Windows卸下它,我会吃掉我的帽子的。微软竭尽全力保持向后兼容性。因此,在我看来, SHFileOperation 是您的最佳选择。

Well, for starters, SHFileOperation has no compatibility issues on Windows 7 or Windows 8. Yes, you are now recommended to use IFileOperation instead. But if you want to support older operating systems like XP, then you can and should just call SHFileOperation. It works and will continue to work. It's pefectly fine to use it on Windows 7 and Windows 8 and I'll eat my hat if it's ever removed from Windows. Microsoft go to extraordinary lengths to maintain backwards compatibility. So, SHFileOperation is your best option in my view.

您的 FindFirst 的方法失败,因为您需要将其放在单独的函数中以允许递归。我在另一个答案中发布的代码不完整。这是完整的版本:

Your FindFirst based approach fails because you need to put it in a separate function in order to allow recursion. And the code I posted in that other answer is incomplete. Here is a complete version:

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;

这将删除目录及其内容。您想走到顶层目录,然后为找到的每个子目录调用此函数。

This deletes a directory and its contents. You'd want to walk the top level directory and then call this function for each subdirectory that you found.

这篇关于删除具有非空子目录和文件的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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