Delphi 中如何递归删除空目录

oknwwptz  于 2022-11-04  发布在  其他
关注(0)|答案(3)|浏览(210)

一个父目录D:\AAA有2个子空目录D:\AAA\BB1和D:\AAA\BB2我的要求是如何递归地删除空目录。这里有两个函数在互联网上找到如下://递归移除空目录

function RemoveEmptyDirectory(path: string) : Boolean;
var
  MySearch: TSearchRec;
  Ended: Boolean;
begin
  if FindFirst(path + '\*.*', faDirectory, MySearch) = 0 then
  begin
    repeat
      if ((MySearch.Attr and faDirectory) = faDirectory) and
        (MySearch.Name[1] <> '.') then
      begin
        if DirectoryIsEmpty(path + '\' + MySearch.Name) then
          TDirectory.Delete(path + '\' + MySearch.Name)
        else
        begin
          RemoveEmptyDirectory(path + '\' + MySearch.Name);
          if DirectoryIsEmpty(path + '\' + MySearch.Name) then
            RemoveEmptyDirectory(path + '\' + MySearch.Name);
        end;
      end;
    until FindNext(MySearch) <> 0;
    FindClose(MySearch);
  end;
end;

//检查目录是否为空

function DirectoryIsEmpty(Directory: string): Boolean;
var
  SR: TSearchRec;
  i: Integer;
begin
  Result := False;
  FindFirst(IncludeTrailingPathDelimiter(Directory) + '*', faAnyFile, SR);
  for i := 1 to 2 do
    if (SR.Name = '.') or (SR.Name = '..') then
      Result := FindNext(SR) <> 0;
  FindClose(SR);
end;

我的问题就在这里:第一次运行函数RemoveEmptyDirectory会发现D:\AAA不为空,然后会运行发送轮(递归方式),删除2个子目录D:\AAA\BB1和D:\AAA\BB2后,父目录会变成一个空目录,返回第一轮时函数DirectoryIsEmpty报告父目录不是空目录!!!!!!为什么!!!!Windows系统还是不改变目录状态???
那么,有没有什么好的建议可以满足我的要求呢?

gdx19jrr

gdx19jrr1#

你从来没检查过自己
最后只做检查和删除:

function RemoveEmptyDirectory(path: string) : Boolean;
var
  MySearch: TSearchRec;
  Ended: Boolean;
begin
  if FindFirst(path + '\*.*', faDirectory, MySearch) = 0 then
  begin
    repeat
      if ((MySearch.Attr and faDirectory) = faDirectory) and
        (MySearch.Name[1] <> '.') then
      begin
        if DirectoryIsEmpty(path + '\' + MySearch.Name) then
          TDirectory.Delete(path + '\' + MySearch.Name)
        else
        begin
          RemoveEmptyDirectory(path + '\' + MySearch.Name);
          if DirectoryIsEmpty(path + '\' + MySearch.Name) then
            RemoveEmptyDirectory(path + '\' + MySearch.Name);
        end;
      end;
    until FindNext(MySearch) <> 0;
    FindClose(MySearch);
  end;

  if DirectoryIsEmpty(path) then
    TDirectory.Delete(path);

end;
klh5stk1

klh5stk12#

您可以将TDirectory用作

TDirectory.Delete('D:\AAA', True);

如果需要检查目录是否为空,可以使用TDirectory.GetDirectories()作为

Var
  S: string;
begin
  for S in TDirectory.GetDirectories('D:\AAA', '*', TSearchOption.soAllDirectories) do
    begin
      if TDirectory.IsEmpty(S) then
        TDirectory.Delete(S);
    end;
  If TDirectory.IsEmpty('D:\AAA') then
    TDirectory.Delete('D:\AAA');
yyhrrdl8

yyhrrdl83#

我认为这是简单明了的,如果顶级性能不是关键的话,应该可以做到:

procedure RemoveEmptyDirs;
var
  i,Removed:integer;
  Arr:TStringDynArray;
const
  TargedDir = 'C:\BunchOfDirs\';
begin
  Arr := TDirectory.GetDirectories(TargedDir,'*',TSearchOption.soAllDirectories);
  Repeat
    Removed := 0;
    For i := High(Arr) downto Low(Arr) do begin
      If TDirectory.IsEmpty(Arr[i]) then begin
        TDirectory.Delete(Arr[i]);
        System.Delete(Arr,i,1);
        Inc(Removed);
      end;
    end;
  Until Removed = 0;
end;

相关问题