如何删除一个包含一些文件和一些非空子目录的目录。
我试过SHFileOperation Function。它在Windows 7中存在一些兼容性问题。
然后我尝试了 IFileOperation Interface。但它在Windows XP中不兼容。然后我按照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、mask和recursive的 Undeclared Identifier 。我的要求是“如果 WALLPAPER 文件夹下存在任何子文件夹,它将被删除”。同一个子文件夹可能包含任意数量的非空子文件夹或文件。