Mitchell Hu
Mitchell Hu

Reputation: 95

How to remove empty directory recursively in Delphi

A parent directory D:\AAA has 2 child empty Directory D:\AAA\BB1 and D:\AAA\BB2 my requirement is how to remove empty Directory recursively. Here are two function found on internet as below : //remove empty Directory recursively

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;

// check directory is empty or not

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;

My problem is here : at first run function RemoveEmptyDirectory will found D:\AAA is not empty, then will run send round (recursively way), After remove 2 child directory D:\AAA\BB1 and D:\AAA\BB2, the parent will become an empty Directory, Back to first round place the function DirectoryIsEmpty report the parent is not an empty directory!!!! Why !!!! Is windows system still not change the directory state ???

So, is there any good suggestion that could meet my requirement.

Upvotes: 2

Views: 1226

Answers (3)

stackmik
stackmik

Reputation: 151

I think this is simple and straightforward and should do fine if top performance is not crucial:

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;

Upvotes: 0

Ilyes
Ilyes

Reputation: 14928

You can use TDirectory as

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

If you need to check if the directories are empty or not, you can use TDirectory.GetDirectories() as

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');

Upvotes: 1

MBo
MBo

Reputation: 80187

You never check D:\AAA itself.

Just make checking and deletion in the end:

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;

Upvotes: 2

Related Questions