procedure DeleteFiles (Path,AFileName: string; recursive:boolean);
var
Result:integer;
Sr:TSearchRec;
begin
if Path[length(path)]<>'\' then
Path:=path+'\';
Result:=FindFirst(Path+AFileName,faAnyFile-faDirectory,Sr);
while Result = 0 do
begin
if not DeleteFile (Path + SearchRec.name) then
begin
FileSetAttr (Path + SearchRec.name, 0); { reset all flags }
DeleteFile (Path + SearchRec.name);
end;
Result := FindNext(SearchRec);
end;
FindClose(SearchRec);
if not recursive then
exit;
Result := FindFirst(Path + '*.*', faDirectory, SearchRec);
while Result = 0 do
begin
if (SearchRec.name <> '.') and (SearchRec.name <> '..') then
begin
FileSetAttr (Path + SearchRec.name, faDirectory);
DeleteFiles (Path + SearchRec.name + '\', Mask, TRUE);
RmDir (Path + SearchRec.name);
end;
Result := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
var
Result:integer;
Sr:TSearchRec;
begin
if Path[length(path)]<>'\' then
Path:=path+'\';
Result:=FindFirst(Path+AFileName,faAnyFile-faDirectory,Sr);
while Result = 0 do
begin
if not DeleteFile (Path + SearchRec.name) then
begin
FileSetAttr (Path + SearchRec.name, 0); { reset all flags }
DeleteFile (Path + SearchRec.name);
end;
Result := FindNext(SearchRec);
end;
FindClose(SearchRec);
if not recursive then
exit;
Result := FindFirst(Path + '*.*', faDirectory, SearchRec);
while Result = 0 do
begin
if (SearchRec.name <> '.') and (SearchRec.name <> '..') then
begin
FileSetAttr (Path + SearchRec.name, faDirectory);
DeleteFiles (Path + SearchRec.name + '\', Mask, TRUE);
RmDir (Path + SearchRec.name);
end;
Result := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
删除一子目录及其下面的文件
The following example demonstrates deleting all the files in a directory and then the directory itself. Additional processing would be required to delete read only files and files that are in use. procedure TForm1.Button1Click(Sender: TObject);
var
DirInfo: TSearchRec;
r : Integer;
begin
r := FindFirst('C:\Download\Test\*.*', FaAnyfile, DirInfo);
while r = 0 do
begin
if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
(DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
if DeleteFile(pChar('C:\Download\test\' + DirInfo.Name)) = false then
ShowMessage('Unable to delete : C:\Download\test\' + DirInfo.Name);
r := FindNext(DirInfo);
end;
SysUtils.FindClose(DirInfo);
if RemoveDirectory('C:\Download\Test') = false then
ShowMessage('Unable to delete direcotry : C:\Download\test');
end;
问题2:
两个procedure 的基本内是一样的,你可以将相同的写成一个function或procedure来调用就可以了。
function unzip(FilePath:stirng;ExtraPath:string):boolean;
begin
if fileexists(FilePath) then
begin
With cxzip do
begin
cxzip.ZipName:=FilePath;
cxzip.DestDir:=ExtraPath;
cxzip.doall:=True;
try
ReadZip;
UnZip;
Except
MessageDlg('文件解压缩失败!',mtError,[mbOk],0);
result :=false;
Exit;
end;
result :=true;
end;
end
else
begin
MessageDlg('要解压的文件不存在!',mtError,[mbOk],0);
result :=false;
end;
end;
调用:
if upzip('c:\zipfiles\'+date+'478.zip','c:\cxbb\txt')=true then
MessageDlg('文件解压缩成功!',mtInformation,[mbOk],0);其它的自已相应改一下吧!