Would the System.IOUtils TDirectory.Delete() work for you ?
It has the "Recursive" parameter option.
class procedure TDirectory.Delete(const Path: string; const Recursive: Boolean);
var
PostCallback: TDirectoryWalkProc;
begin
CheckDeleteParameters(Path, Recursive);
if Recursive then
begin
PostCallback :=
function (const Path: string; const FileInfo: TSearchRec): Boolean
var
CompletePath: string;
begin
Result := True;
if (FileInfo.Name <> TPath.FCParentDir) and (FileInfo.Name <> TPath.FCCurrentDir) then
begin
CompletePath := TPath.DoCombine(Path, FileInfo.Name, False);
// clear read-only, system and hidden attributes that can compromise
// the deletion
{$IFDEF MSWINDOWS}
FileSetAttr(CompletePath, System.SysUtils.faNormal);
{$ENDIF MSWINDOWS}
case FileInfo.Attr and System.SysUtils.faDirectory of
System.SysUtils.faDirectory: // remove empty directories
RemoveDir(CompletePath);
0: // remove files
DeleteFile(CompletePath);
end;
end;
end;
// delete all the files and subdirectories
WalkThroughDirectory(Path, '*', nil, PostCallback, Recursive); // DO NOT LOCALIZE
end;
// delete the directory itself
{$IFDEF MSWINDOWS}
FileSetAttr(Path, System.SysUtils.faNormal);
{$ENDIF}
RemoveDir(Path);
end;
BTW: It has also a CreateDirectory, which uses ForceDirectories(FullPath); and seems a little bit more reliable
class procedure TDirectory.CreateDirectory(const Path: string);
var
FullPath: string;
begin
FullPath := TPath.DoGetFullPath(Path);
CheckCreateDirectoryParameters(FullPath);
ForceDirectories(FullPath);
end;