Sending files to the recycle bin

This article shows how to send a file to the recyle bin rather than deleting immediately.

unit RecycleBin;



interface



uses

  Windows, ShellApi;



function RecycleFile(Filename: string; const Prompt: Boolean): Boolean;

function RecycleFiles(Files: TStrings; const Prompt: Boolean): Boolean;



implementation



function RecycleFile(Filename: string; const Prompt: Boolean): Boolean;

var

   Struct: TSHFileOpStruct;

   Tmp: string;

   ResultVal: Integer;

begin

   Tmp := FileName+#0#0;

   Struct.wnd := 0;

   Struct.wFunc := FO_DELETE;

   Struct.pFrom := PChar(Tmp);

   Struct.pTo := nil;

   Struct.fFlags:= FOF_ALLOWUNDO;

   if not Prompt then

      Struct.fFlags := Struct.fFlags or FOF_NOCONFIRMATION;



   Struct.fAnyOperationsAborted := False;

   Struct.hNameMappings := nil;

   ResultVal := ShFileOperation(Struct);

   Result := (Resultval = 0);

end;



function RecycleFiles(Files: TStrings; const Prompt: Boolean): Boolean;

var

  I: Integer;

  AllFiles: string;

  NextPos: PChar;

begin

  Result := False;

  if Files.Count = 0 then Exit;

  SetLength(AllFiles, Length(Files.Text));

  FillChar(AllFiles[1], Length(AllFiles), 0);

  NextPos := @AllFiles[1];

  for I := 0 to Files.Count - 1 do

  begin

    Move(PChar(Files[I])^, NextPos^, Length(Files[I]));

    Inc(NextPos, Length(Files[I]) + 1);

  end;

  SetLength(AllFiles, NextPos - @AllFiles[1]);

  Result := RecycleFile(AllFiles, Prompt);

end;

 

Share this article!

Follow us!

Find more helpful articles: