Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanently Delete Directories

I use this code to delete whole directories:

uses
  ShellApi;

function DelDir(dir: string): Boolean;
var
  fos: TSHFileOpStruct;
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do
  begin
    wFunc  := FO_DELETE;
    fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
    pFrom  := PChar(dir + #0);
  end;
  Result := (0 = ShFileOperation(fos));
end;

Are there any flags I can set to allow for permanent removal of deleted directories?
By permanent removal, I mean it won't show up in the recycle bin after it is deleted because this is what happens when I use the DelDir Function.

like image 636
ple103 Avatar asked Oct 11 '22 11:10

ple103


1 Answers

Try to set

FileOpStruct.pTo := nil;

Example:

function DeleteTree(const APath: String): Boolean;
var
  FileOpStruct : TShFileOpStruct;
  ErrorCode: Integer;
begin
  Result := False;
  if DirectoryExists(APath) then begin
    FillChar(FileOpStruct, SizeOf(FileOpStruct), #0);
    FileOpStruct.Wnd := 0;
    FileOpStruct.wFunc := FO_DELETE;
    FileOpStruct.pFrom := PChar(APath + #0#0);
    FileOpStruct.pTo := nil;
    FileOpStruct.fFlags := FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_NOCONFIRMMKDIR;
    FileOpStruct.lpszProgressTitle := nil;
    ErrorCode := ShFileOperation(FileOpStruct);
    Result := ErrorCode = 0;
  end;
end;
like image 84
homolibere Avatar answered Oct 15 '22 09:10

homolibere