Peter Luijer"s Entry for the Custom RemoveEmptyFolders Delphi Function
Question: Peter Luijer's Entry for the Custom RemoveEmptyFolders Delphi Function
Challenge: a custom Delphi function, RemoveEmptyFolders, should take a path name (directory) and should remove / delete all empty sub-folders at any (sub) level.
The code is submitted for the RemoveEmptyFolders Delphi Challenge
Answer:
RemoveEmptyFolders entry by Peter Luijer (The Netherlands):
Explore the list of all accepted entries for RemoveEmptyFolders Delphi challenge.
Challenge: a custom Delphi function, RemoveEmptyFolders, should take a path name (directory) and should remove / delete all empty sub-folders at any (sub) level.
The code is submitted for the RemoveEmptyFolders Delphi Challenge
Answer:
RemoveEmptyFolders entry by Peter Luijer (The Netherlands):
Procedure RemoveEmptyFolders(Const RootFolder : String) ; Function ScanPath(Folder : String; List : TStrings) : Integer; Procedure ProcessPath(Rec : TSearchRec) ; Begin If (Rec.Name <> '.') And (Rec.Name <> '..') Then If (Rec.Attr And faDirectory > 0) Then Inc(Result, ScanPath(Folder + Rec.Name, List)) Else Inc(Result) ; End; Var R : TSearchRec; Begin Result := 0; Folder := IncludeTrailingPathDelimiter(Folder) ; If (FindFirst(Folder + '*.*', faAnyFile, R) = 0) Then Try ProcessPath(R) ; While (FindNext(R) = 0) Do ProcessPath(R) ; If (Result = 0) Then List.Add(Folder) ; Finally FindClose(R) ; End; End;Var K : Integer; L : TStringList; Begin L := TStringList.Create; Try L.Sorted := True; K := Pred(ScanPath(RootFolder, L)) ; While (K >= 0) Do Begin RemoveDir(L[K]) ; Dec(K) ; End; Finally L.Free; End; End;
Explore the list of all accepted entries for RemoveEmptyFolders Delphi challenge.
Source...