问题有二
一 这个过程删除文件夹,如果文件过多就会暂时假死,有什么办法控制有节制的删除文件。二 过程在删除文件夹的同时像一个memo或者其他的控件,输出正在删除的文件。procedure DeleteEmptyFolder(FolderName:string);
var
sr:TSearchRec;
sPath, sFile:string;
begin
if Copy(FolderName, Length(FolderName), 1) <> '\' then //???? '\'
sPath := FolderName + '\'
else
sPath := FolderName;
//------------------------------------------------------------------
if FindFirst(sPath + '*.*', faAnyFile, sr) = 0 then
begin
repeat
sFile := Trim(sr.Name);
if sFile = '.' then Continue;
if sFile = '..' then Continue;
sFile := sPath + sr.Name;
if (sr.Attr and faDirectory) <> 0 then
DeleteEmptyFolder(sFile)
else if (sr.Attr and faAnyFile) = sr.Attr then
FileSetAttr(sFile,32);
DeleteFile(sFile); // ??????
DeleteEmptyFolder(sFile);
until FindNext(sr) <> 0;
FindClose(sr);
end;
一 这个过程删除文件夹,如果文件过多就会暂时假死,有什么办法控制有节制的删除文件。二 过程在删除文件夹的同时像一个memo或者其他的控件,输出正在删除的文件。procedure DeleteEmptyFolder(FolderName:string);
var
sr:TSearchRec;
sPath, sFile:string;
begin
if Copy(FolderName, Length(FolderName), 1) <> '\' then //???? '\'
sPath := FolderName + '\'
else
sPath := FolderName;
//------------------------------------------------------------------
if FindFirst(sPath + '*.*', faAnyFile, sr) = 0 then
begin
repeat
sFile := Trim(sr.Name);
if sFile = '.' then Continue;
if sFile = '..' then Continue;
sFile := sPath + sr.Name;
if (sr.Attr and faDirectory) <> 0 then
DeleteEmptyFolder(sFile)
else if (sr.Attr and faAnyFile) = sr.Attr then
FileSetAttr(sFile,32);
DeleteFile(sFile); // ??????
DeleteEmptyFolder(sFile);
until FindNext(sr) <> 0;
FindClose(sr);
end;
Application.ProcessMessages
一次
这个递归过程在文件太多的情况下肯定会出现卡死现象,楼上记为的方法Application.ProcessMessages ,可以缓解这个问题的,也同样可以解决你的第二问题
对于第一个问题,其实你可以调用api去删除文件或文件夹,也就是windows本身的删除,而且这样还可以有提示,代码如下function DeleteFolder(ProName:string): Boolean;
var
SHFileOpStruct: TSHFileOpStruct;
pName: string;
ret: integer;
begin
if ProName = '' then
begin
Application.MessageBox('没有工程可以删除','',MB_OK);
Exit;
end;
pName := ProName + #0#0;
with SHFileOpStruct do
begin
Wnd := Application.Handle;
wFunc := FO_DELETE;
pFrom := pchar(pName);
pTo := nil;
fFlags := {FOF_NOCONFIRMATION + }FOF_SILENT + FOF_NOERRORUI;
// if Recycle then fFlags := fFlags + FOF_ALLOWUNDO
end;
ret := SHFileOperation(SHFileOpStruct);
Result := ret = 0;
end;