【发布时间】:2013-05-02 11:20:48
【问题描述】:
如何删除一个包含一些文件和一些非空子目录的目录。
我试过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 处编译提及错误作为未声明的标识符。我的要求是“如果 WALLPAPER 文件夹下存在任何子文件夹,它将被删除”。同一个子文件夹可以包含任意数量的非空子文件夹或文件。
【问题讨论】:
标签: delphi