当我在文件夹中有子文件夹时 - 此代码不是删除文件夹...是否有任何错误?
procedure TForm.Remove(Dir: String);
var
Result: TSearchRec; Found: Boolean;
begin
Found := False;
if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
while not Found do begin
if (Result.Attr and faDirectory = faDirectory) AND (Result.Name <> '.') AND (Result.Name <> '..') then Remove(Dir + '\' + Result.Name)
else if (Result.Attr and faAnyFile <> faDirectory) then DeleteFile(Dir + '\' + Result.Name);
Found := FindNext(Result) <> 0;
end;
FindClose(Result); RemoveDir(Dir);
end;
Run Code Online (Sandbox Code Playgroud) 如何删除一个包含某些文件和一些非空子目录的目录.
我试过SHFileOperation函数.它在Windows 7中存在一些兼容性问题.
然后我尝试了IFileOperation接口.但它在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); …Run Code Online (Sandbox Code Playgroud)