如何删除包含一些文件和一些非空子目录的目录。我试过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,面具和递归。我的要求是“如果WALLPAPER文件夹下存在任何子文件夹,它将被删除”。同一个子文件夹可能包含任意数量的非空子文件夹或文件。
@ user235 ....操作系统不会让一个正常的应用程序从c:\ windows删除文件! – Ampere 2014-04-08 11:31:43