Well, for starters, SHFileOperation
has no compatibility issues in Windows 7 or Windows 8. Yes, now you are advised to use IFileOperation
. But if you want to support older operating systems such as XP, you can and should just call SHFileOperation
. It works and will continue to work. This is great for use on Windows 7 and Windows 8, and I will eat a hat if it ever removes it from Windows. Microsoft has extraordinary lengths to support backward compatibility. So SHFileOperation
is your best option in my opinion.
Your FindFirst
based approach fails because you need to put it in a separate function to enable recursion. And the code I wrote in this other answer is incomplete. Here is the full version:
procedure DeleteDirectory(const Name: string); var F: TSearchRec; begin if FindFirst(Name + '\*', faAnyFile, F) = 0 then begin try repeat if (F.Attr and faDirectory <> 0) then begin if (F.Name <> '.') and (F.Name <> '..') then begin DeleteDirectory(Name + '\' + F.Name); end; end else begin DeleteFile(Name + '\' + F.Name); end; until FindNext(F) <> 0; finally FindClose(F); end; RemoveDir(Name); end; end;
This removes the directory and its contents. You want to go to the top level directory and then call this function for each subdirectory found.
source share