procedure DelTree(sPath: string);
var
hFind: THandle;
filename: string;
fd: WIN32_FIND_DATA;
bDelete: BOOL;
i: Integer;
begin
if sPath='' then
exit;
filename:=sPath+'\*.*';
hFind:=Windows.FindFirstFile(PChar(filename), fd);
if(hFind <> INVALID_HANDLE_VALUE) then
begin
while Windows.FindNextFile(hFind, fd) do
begin
filename:=fd.cFileName;
if((filename = '.') or (filename = '..')) then
Continue;
if(fd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) > 0 then
begin
//子目錄
DelTree(sPath+'\'+filename);
end
else
begin
//Full filename.
filename:=sPath+'\'+filename;
if(not SetFileAttributes(PChar(filename),
FILE_ATTRIBUTE_NORMAL)) then
begin
//SaveLog('Set '+filename+' file attribute to normal error !');
end;
i:=0;
bDelete:=DeleteFile(PChar(filename));
while((i<5) and (not bDelete)) do
begin
Sleep(1000);
bDelete:=DeleteFile(PChar(filename));
Inc(i);
end;
if(not bDelete) then
begin
//SaveLog('Delete file '+filename+' error !');
end;
end;
end;
Windows.FindClose(hFind);
end;
if(not SetFileAttributes(PChar(sPath),
FILE_ATTRIBUTE_NORMAL)) then
begin
//SaveLog('Set '+sPath+' directory attribute to normal error !');
end;
//SaveLog('Remove directory '+sPath);
if(not RemoveDirectory(PChar(sPath))) then
begin
//SaveLog('Remove directory '+sPath+' error !');
end;
//SaveLog('CcyDelTree '+sPath+' completely.');
end;