Deleting a directory or set of files
   


DeleteAllFiles() will delete a set of files or a directory of your choice and place them in the Recycle Bin. It also displays the same animation of the operation as does Windows Explorer. You can pass wildcards to the function. In order to delete a directory, do not include the last backslash. For example: DeleteAllFiles('C:\ThisDirectory');
Raghavendra Rao
gave me a fix for an AV that sometimes occurred on NT systems: add FillChar() call at the beginning of the function.

Update: this update fixes the "System File Error: 1026" that sometimes appeared.


uses ShellAPI;

function DeleteAllFiles(FilesOrDir: string): boolean;
{ Sends files or directory to the recycle bin. }
var

  F: TSHFileOpStruct;
  From: string;
  Resultval: integer;
begin
  FillChar(F, SizeOf(F), #0);
  From := FilesOrDir + #0;
  Screen.Cursor := crHourGlass;

  try
    F.wnd := 0;
    F.wFunc := FO_DELETE;
    F.pFrom := PChar(From);
    F.pTo := nil;
    F.fFlags := FOF_ALLOWUNDO or
                FOF_NOCONFIRMATION or
                FOF_SIMPLEPROGRESS or
                FOF_FILESONLY;
    F.fAnyOperationsAborted := False;
    F.hNameMappings := nil;
    Resultval := ShFileOperation(F);
    Result := (ResultVal = 0);
  finally
    Screen.Cursor := crDefault;
  end;
end;


Blue Orb Software

[email protected]