Delphi,删除包含内容的文件夹
当我的文件夹中有子文件夹时 - 此代码不会删除文件夹...有任何错误吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最简单的方法是调用
TDirectory.Delete(Dir, True)
。TDirectory
在IOUtils 这是最近添加的 RTL。
True
标志传递给Recursive
参数,这意味着在删除目录之前清空目录的内容,这是删除目录的重要部分。在评论中,您告诉我们您使用的是 Delphi 7,因此无法使用它。
你的代码看起来大部分都很好。但是,您的意思并不是:
我认为您的意思是:
我可能会写如下:
The simplest thing to do is to call
TDirectory.Delete(Dir, True)
.TDirectory
is found inIOUtils
which is quite a recent RTL addition.The
True
flag is passed to theRecursive
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:
I think you mean:
I would probably write it as follows:
如果我是您,我只会告诉操作系统删除该文件夹及其所有内容。通过编写 (
uses ShellAPI
) 来做到这一点[如果你
这样做,你会得到一个很好的确认对话框。如果这样做,
您不会看到确认对话框,但如果操作很长,您会看到一个进度条。最后,如果添加
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
)[If you do
instead, you get a nice confirmation dialog. If you do
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.Of course, you can combine flags as you like:
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.]上次我需要删除包含内容的文件夹时,我使用了 JCL :
最后一个参数告诉我们文件是否应该进入回收站,这是一个很好的好处。
The last time I needed to delete a folder with content I used the JCL:
The last parameter tells whether the files should go to the recycle bin or not, which is a nice bonus.
要解决最初的问题 - 尝试以下操作:
To address the original problem - try this:
DSiWin32 是一个开源项目,具有“随心所欲使用”许可证。
DSiWin32 is open source project relased with "use as you wish" license.
2022: System.IOUtils.TDirectory.Delete : )
2022: System.IOUtils.TDirectory.Delete :)
此过程适用于不同的格式:
This procedure works for different formats: