【发布时间】:2011-04-19 12:55:22
【问题描述】:
当我在文件夹中有子文件夹时 - 此代码不是删除文件夹...是否有任何错误?
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;
【问题讨论】:
-
顺便说一句,这段代码可以写成重复直到循环,从而避免需要
Found局部变量。 -
另外,如果找到文件,
Found是false,如果没有找到文件,则true有点好笑…… -
如果以下任何答案解决了您的问题,您应该通过单击答案左侧的复选标记来接受它。如果不止一个答案解决了您的问题,请选择“最佳”的答案。