Delphi,删除文件夹与内容 [英] Delphi, delete folder with content

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

问题描述

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

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;


推荐答案

如果我是你,我只是告诉操作系统删除其所有内容的文件夹。通过写入(使用ShellAPI

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);

[如果你这样做

  ShOp.fFlags := 0;

相反,你会得到一个很好的确认对话框。如果你执行

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

ShOp.fFlags := FOF_NOCONFIRMATION;

你没有得到确认对话框,但是如果操作很长,你会得到一个进度条。最后,如果添加 FOF_ALLOWUNDO 标志,则将目录移动到废物仓,而不是永久删除。

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;

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

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

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

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