如何刪除包含一些文件和一些非空子目錄的目錄。我試過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