删除文件和目录(Delphi实现)

//删除文件和目录
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;    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值