5

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.

Kromster
  • 7,181
  • 7
  • 63
  • 111
ple103
  • 2,080
  • 6
  • 35
  • 50
  • 2
    What is permament removal? So that it cannot be recreated? – Jacob Jul 20 '11 at 06:40
  • Sorry. By permanent removal, I mean it won't show up in the recycle bin. – ple103 Jul 20 '11 at 06:42
  • 1
    possible duplicate of [Delphi, delete folder with content](http://stackoverflow.com/questions/5716666/delphi-delete-folder-with-content) You can see here how to delete a folder without going to the wastebin. Hint: use TDirectory.delete. – Johan Jul 20 '11 at 07:28
  • 3
    hm, looks like it should delete permanently by default and there's a flag FOF_ALLOWUNDO to disallow this. What if you try to force reset this bit? Some more info can be found [here](http://stackoverflow.com/questions/5716666/delphi-delete-folder-with-content). – Dmitry Polomoshnov Jul 20 '11 at 07:28
  • @Johan: The question is not really a duplicate (it asked how to delete folders as well as files), though the accepted answer does hold the answer to this question. – Marjan Venema Jul 20 '11 at 07:46

1 Answers1

4

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;
homolibere
  • 56
  • 2
  • I think it's enough with two null terminators (in `pFrom`). – Andreas Rejbrand Jul 20 '11 at 07:49
  • 2
    Your suggested change does nothing. The original code already uses ZeroMemory to set all the fields, which effectively sets pTo to nil. What's the problem in the original code, and how does yours fix it? – Rob Kennedy Jul 20 '11 at 13:01