获取Windows的ShFileOperation API以递归删除Delphi中的文件 [英] Getting windows ‘ShFileOperation’ API to recursively delete files in Delphi
问题描述
我正在使用以下代码删除大量文件。
I am using the following code to delete a large number of files
function FastDelete(const fromDir: string): Boolean;
var
fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do
begin
wFunc := FO_DELETE;
fFlags := FOF_FILESONLY or
FOF_NOCONFIRMATION or
FOF_NO_CONNECTED_ELEMENTS or
FOF_NOERRORUI or
FOF_NO_UI;
pFrom := PChar(fromDir+'\*.*' + #0);
end;
Result := (0 = ShFileOperation(fos));
end;
如何让它递归删除路径中的所有文件?
How do I get it to recursively delete all the files in the path?
编辑
问题是 FOF_FILESONLY flag
删除后文件被递归删除
The problem is the FOF_FILESONLY flag After removing it files are recursively deleted
推荐答案
从 MSDN文档:
FOF_NORECURSION
仅在本地目录中执行操作。不要递归地进入子目录这是默认行为。
Only perform the operation in the local directory. Don't operate recursively into subdirectories, which is the default behavior.
看起来就是你的答案那里。它应该自动递归,除非你不要求它。
Looks like that's your answer right there. It should recurse automatically unless you ask it not to.
编辑:看起来你的标志有问题。您需要将或在一起,而不是将它们添加在一起。由于 FOF_NO_UI
已经包含 FOF_NOERRORUI
,再次添加可以更改该值,您可能会不小心将某些事物添加在一起加起来 FOF_NORECURSION
。它应该是这样的:
Looks like there's a problem with your flags. You need to OR them together, not add them together. Since FOF_NO_UI
already includes FOF_NOERRORUI
, adding it again can change the value, and you might be accidentally adding some things together that add up to FOF_NORECURSION
. It should look like this:
fFlags := FOF_FILESONLY or
FOF_NOCONFIRMATION or
FOF_NO_CONNECTED_ELEMENTS or
FOF_NOERRORUI or
FOF_NO_UI;
这篇关于获取Windows的ShFileOperation API以递归删除Delphi中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!