La función que presentas no es recursiva, por lo que si la carpeta tiene subcarpeta/s no funcionará.
Además de esto, si el archivo empieza por "." tampoco lo borrará. Esto puede darse en algunos casos en de aplicaciones portadas de Linux.
Te dejo a continuación tu función, corregida para ser recursiva y con los pasos comentados para aclarar qué es lo que hace en cada paso.
Verás que he borrado unas lineas que hacen referencia a Clipboard que parece que no tienen relación con el borrado de una carpeta.
Código Delphi
[-]
function RemoveFolder(const Folder : string): Boolean;
var
szFile : string;
SearchRec: TSearchRec;
szSearchPath : string;
nResult : integer;
Attributes: LongWord;
begin
Result := False;
szSearchPath := Folder;
nResult := FindFirst(szSearchPath + '\*.*', faAnyFile, SearchRec);
try
while nResult = 0 do
begin
if not ((SearchRec.Name = '.') or (SearchRec.Name = '..')) then
begin
szFile := szSearchPath + '\' + SearchRec.Name;
if ((SearchRec.Attr and faDirectory) = faDirectory) then
RemoveFolder(szFile)
else
begin
FileSetAttr(szFile, 0);
DeleteFile(PAnsiChar(szFile));
end;
end;
nResult := FindNext(SearchRec);
end;
Result := True;
finally
SysUtils.FindClose(SearchRec);
end;
Attributes := GetFileAttributes(PChar(Folder));
if (Attributes = $FFFFFFFF) then
raise EInOutError.Create(SysErrorMessage(GetLastError));
if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
SetFileAttributes(PChar(Folder), Attributes and not FILE_ATTRIBUTE_READONLY);
if Windows.RemoveDirectory(PChar(Folder)) = False then
raise EInOutError.Create(SysErrorMessage(GetLastError));
end;
Nota:
Tengo esta estructura
Código:
c:\Borrame\Prueba
A.txt
B.txt
Subcarpeta
C.txt
D.txt
y lo llamo :
Código Delphi
[-]
RemoveFolder('c:\Borrame\Prueba');