var sr: TSearchRec; begin try if FindFirst(sDir + '*.RED', faAnyFile and not faDirectory, sr) = 0 then begin repeat //你的操作 (sDir + sr.Name)就是当前找到的文件名 until FindNext(sr) <> 0; FindClose(sr); end except end; end;
递归实现删除某一目录下所有文件 var Form1: TForm1; rec_stack:array [1..30] of TSearchRec; rec_pointer:integer; Del_Flag:Boolean; ------------------------------------------------------------------ ------procedure TForm1.DeleteTree(s:string); VAR searchRec:TSearchRec; begin if FindFirst(s+'\*.*', faAnyFile, SearchRec)=0 then repeat if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then begin if (SearchRec.Attr and faDirectory>0) then begin rec_stack[rec_pointer]:=SearchRec; rec_pointer:=rec_pointer-1; DeleteTree(s+'\'+SearchRec.Name); rec_pointer:=rec_pointer+1; SearchRec:=rec_stack[rec_pointer]; end else begin try FileSetAttr(s+'\'+SearchRec.Name,faArchive); DeleteFile(s+'\'+SearchRec.Name); except Application.MessageBox(PChar('Delete File:'+s+'\'+SearchRec.Name+' Error!'),'Info',MB_OK); Del_Flag:=False; end; end; end; until (FindNext(SearchRec)<>0); FindClose(SearchRec); if rec_pointer<30 then begin try FileSetAttr(s,faArchive); RemoveDir(s); except Application.MessageBox(PChar('Delete Directory:'+s+' Error!'),'Info',MB_OK); Del_Flag:=False; end; end; end; ---------------------------------------------------------Del_Flag:=True; rec_pointer:=30; DeleteTree('c:\temp'); if Del_Flag then Application.MessageBox(PChar('目录c:\temp的内容已成功清除!'),'信息',MB_OK);
if FindFirst(sDir + '*.RED', faAnyFile and not faDirectory, sr) = 0 then 改为if FindFirst(sDir + '*.*', faAnyFile and faDirectory, sr) = 0 then 遍历所有文件
sr: TSearchRec;
begin
try
if FindFirst(sDir + '*.RED', faAnyFile and not faDirectory, sr) = 0 then begin
repeat
//你的操作 (sDir + sr.Name)就是当前找到的文件名
until FindNext(sr) <> 0;
FindClose(sr);
end
except
end;
end;
递归实现删除某一目录下所有文件
var Form1: TForm1;
rec_stack:array [1..30] of TSearchRec;
rec_pointer:integer;
Del_Flag:Boolean;
------------------------------------------------------------------ ------procedure TForm1.DeleteTree(s:string);
VAR searchRec:TSearchRec;
begin
if FindFirst(s+'\*.*', faAnyFile, SearchRec)=0 then
repeat
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
if (SearchRec.Attr and faDirectory>0) then
begin
rec_stack[rec_pointer]:=SearchRec;
rec_pointer:=rec_pointer-1;
DeleteTree(s+'\'+SearchRec.Name);
rec_pointer:=rec_pointer+1;
SearchRec:=rec_stack[rec_pointer];
end
else
begin
try
FileSetAttr(s+'\'+SearchRec.Name,faArchive);
DeleteFile(s+'\'+SearchRec.Name);
except
Application.MessageBox(PChar('Delete File:'+s+'\'+SearchRec.Name+' Error!'),'Info',MB_OK);
Del_Flag:=False;
end;
end;
end;
until (FindNext(SearchRec)<>0);
FindClose(SearchRec);
if rec_pointer<30 then
begin
try
FileSetAttr(s,faArchive);
RemoveDir(s);
except
Application.MessageBox(PChar('Delete Directory:'+s+' Error!'),'Info',MB_OK);
Del_Flag:=False;
end;
end;
end;
---------------------------------------------------------Del_Flag:=True;
rec_pointer:=30;
DeleteTree('c:\temp');
if Del_Flag then Application.MessageBox(PChar('目录c:\temp的内容已成功清除!'),'信息',MB_OK);
遍历所有文件