Virtual Pascal

Classic Object Pascal for free

Deleting to the (Windows) Recycle bin rather than ..permanently ... is there a way to do this pls?

G'day All,

Deleting to the (Windows) Recycle bin rather than ..permanently ... is there a way to do this pls?

That says it all I think :-) 

NB: This is running in batch mode - ie: "Compatible with GUI"

At the moment I have to "delete", by "renaming" a file to the folder "\MyRecycleBin" that I set up - vs the one managed in Windows. It's just another folder with an obvious name - nothing special about it.

However this technique at least preserves the original folder structure that the file resided in.

Whilst it works in concept, it would be preferable to delete "correctly" .. ie: conform to the OS in use.

And of course, the folder(s) "\MyRecycleBin" have to be managed as well, ie: not so good.

As far as I'm aware, both "FileDelete" & "Erase" delete the file permanently - not to the recycle bin.

Thoughts welcome pls!

Many Thanks,

Barry

Views: 69

Reply to This

Replies to This Discussion

G'day Barry,

You can use the SHFileOperation function :

Program GotoRecycleBin;

{ Sends a file to the recycle bin.
  Example by Alcatiz - http://alcatiz.developpez.com/ }

Uses Crt,
     Strings,
     Windows;
     
Const (* TSHFILEOPSTRUCT wFunc option *)
      FO_DELETE = $0003;
      (* TSHFILEOPSTRUCT fFlags option *)
      FOF_ALLOWUNDO = $0040;
     
Type TSHFILEOPSTRUCT = Packed Record
       HWindow : HWND;
       wFunc : UINT;
       pFrom : pChar;
       pTo : pChar;
       fFlags : Word;
       fAnyOperationsAborted : LongBool;
       hNameMappings : DWord;
       lpszProgressTitle : pChar;
     end;
     
Var FileName : Array [0..MAX_PATH] of Char;
    SHFileOpStruct : TSHFILEOPSTRUCT;
    Result : Integer;
    
    
{&StdCall+}
Function SHFileOperation(lpFileOp : TSHFILEOPSTRUCT) : Integer; external 'shell32' name 'SHFileOperationA';
{&StdCall-}    

Begin
  (* Parameter test *)
  if ParamCount = 0
     then
       begin
         WriteLn('* Wrong parameter(s) : usage is GotoRecycleBin <file name>.');
       end
     else
       begin
         StrPCopy(FileName,ParamStr(1));
         StrCat(FileName,#0);   (* File name is a double-null terminated string *)
         FillChar(SHFileOpStruct,0,SizeOf(SHFileOpStruct));
         with SHFileOpStruct do
           begin
             wFunc := FO_DELETE;        (* Deletes the file... *)
             pFrom := FileName;
             fFlags := FOF_ALLOWUNDO;   (* ... but sends it to the recycle bin *)
           end;
         Result := SHFileOperation(SHFileOpStruct);
         if Result <> 0
            then
              begin
                Write('* Error: ');
                case Result of
                  $78 : WriteLn('Security settings denied access to the source.');
                  { Other values : https://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85) }
                end;
              end;
     end;
End.

G'day JL,

Thanks for that - I'll give it a go later today.

Cheers

Barry

RSS

© 2017   Created by Allan Mertner.   Powered by

Report an Issue  |  Terms of Service