//删除文件和目录
function Delpath(AFilePath: String): Boolean; stdcall;
var
i: integer;
fpath: String;
PathList: TStringList;
procedure DelFile(AFilePath: String);
var
fpath: String;
srec: TSearchRec;
begin
if Not DirectoryExists(AFilePath) then
Exit;
PathList.Add(AFilePath);
fpath := AFilePath + '/*.*';
if 0 = FindFirst(fpath, faAnyFile, srec) then
begin
if (srec.Name<>'.')and(srec.Name<>'..') then
begin
if (srec.Attr and faDirectory)=faDirectory then
begin
DelFile(AFilePath + '/' + srec.Name);
end
else
DeleteFile(AFilePath + '/' + srec.Name);
end;
while FindNext(srec)=0 do
begin
if (srec.Name<>'.')and(srec.Name<>'..') then
if (srec.Attr and faDirectory)=faDirectory then
DelFile(AFilePath + '/' + srec.Name)
else
DeleteFile(AFilePath + '/' + srec.Name);
end;
end;
FindClose(srec);
end;
begin
Result := False;
if Not DirectoryExists(AFilePath) then
begin
Result := True;
Exit;
end;
PathList := TStringList.Create;
fpath := AFilePath;
if fpath[length(fpath)] = '/' then
fpath := Copy(fpath, 1, length(fpath)-1);
DelFile(fpath);
if PathList.Count > 0 then
for i:=PathList.Count-1 downto 0 do
RmDir(pathlist.Strings[i]);
if Not DirectoryExists(AFilePath) then
Result := True;
end;
function Delpath(AFilePath: String): Boolean; stdcall;
var
i: integer;
fpath: String;
PathList: TStringList;
procedure DelFile(AFilePath: String);
var
fpath: String;
srec: TSearchRec;
begin
if Not DirectoryExists(AFilePath) then
Exit;
PathList.Add(AFilePath);
fpath := AFilePath + '/*.*';
if 0 = FindFirst(fpath, faAnyFile, srec) then
begin
if (srec.Name<>'.')and(srec.Name<>'..') then
begin
if (srec.Attr and faDirectory)=faDirectory then
begin
DelFile(AFilePath + '/' + srec.Name);
end
else
DeleteFile(AFilePath + '/' + srec.Name);
end;
while FindNext(srec)=0 do
begin
if (srec.Name<>'.')and(srec.Name<>'..') then
if (srec.Attr and faDirectory)=faDirectory then
DelFile(AFilePath + '/' + srec.Name)
else
DeleteFile(AFilePath + '/' + srec.Name);
end;
end;
FindClose(srec);
end;
begin
Result := False;
if Not DirectoryExists(AFilePath) then
begin
Result := True;
Exit;
end;
PathList := TStringList.Create;
fpath := AFilePath;
if fpath[length(fpath)] = '/' then
fpath := Copy(fpath, 1, length(fpath)-1);
DelFile(fpath);
if PathList.Count > 0 then
for i:=PathList.Count-1 downto 0 do
RmDir(pathlist.Strings[i]);
if Not DirectoryExists(AFilePath) then
Result := True;
end;
本文介绍了一个用于删除文件和目录的DelPath函数实现方法。该函数通过递归方式遍历指定路径下的所有文件及子目录,并逐一进行删除操作。文章详细解释了如何使用FindFirst和FindNext函数来获取路径下所有文件及目录的信息,并根据是否为目录决定调用自身或其他删除函数。
1141

被折叠的 条评论
为什么被折叠?



