Page 1 of 1

Delete a file to the Windows 10 recycle bin [SOLVED]

Posted: Sat Mar 20, 2021 3:12 pm
by davebar
I have found numerous "Delete to recycle bin" examples in this forum and other sources, but I am unable to make them work.
Does anyone have a procedure that works in PB 5.73 under Windows 10.
TIA
Dave

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 3:18 pm
by BarryG
Post your code so we can see what's wrong. The other examples work for me.

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 3:58 pm
by davebar
The reason for my question is that I do NOT have any code to post.
Here is one of several examples I found:
http://forums.purebasic.com/english/vie ... 13&t=22500
Like most other examples it is quite old and I suspect is not valid for Win 10.

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 4:07 pm
by davebar
BarryG wrote:The other examples work for me.
Can you please point me to one of the other examples that work for you.

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 4:27 pm
by BarryG
davebar wrote:Can you please point me to one of the other examples that work for you.
This one, from the link you posted -> viewtopic.php?p=306504#p306504

To remove the confirmation prompt, I changed one line to this:

Code: Select all

SHFileOp\fFlags=#FOF_ALLOWUNDO|#FOF_NOCONFIRMATION
Works great here on Win 10 Pro.

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 5:05 pm
by davebar
Thanks. Here's my code that does not work:

Code: Select all

Procedure.l RECYCLEFile(File$); - Delete a file and move it in the RECYCLE-Bin 
  SHFileOp.SHFILEOPSTRUCT 
  SHFileOp\pFrom=@File$ 
  SHFileOp\wFunc=#FO_DELETE 
  SHFileOp\fFlags=#FOF_ALLOWUNDO | #FOF_NOCONFIRMATION
  ProcedureReturn SHFileOperation_(SHFileOp) 
EndProcedure

MyFile$ = "D:\Temp\Test.txt"
Debug RECYCLEFile(MyFile$) ; Returns 2
MyFile$ is not deleted and the recycle bin is empty when the execution finishes.

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 5:08 pm
by RASHAD

Code: Select all

Procedure _Recycle(File$)
  SHFileOp.SHFILEOPSTRUCT
  m = AllocateMemory(( Len(File$)+2 ) * SizeOf(Character) )
  If m
    PokeS(m,File$)
    SHFileOp\pFrom  = m
    SHFileOp\wFunc  = #FO_DELETE
    SHFileOp\fFlags = #FOF_ALLOWUNDO|#FOF_SILENT
    result = SHFileOperation_(SHFileOp)
    FreeMemory(m)
  EndIf
EndProcedure

Procedure _Delete(File$)
  SHFileOp.SHFILEOPSTRUCT
  m = AllocateMemory(( Len(File$)+2 ) * SizeOf(Character) )
  If m
    PokeS(m,File$)
    SHFileOp\pFrom  = m
    SHFileOp\wFunc  = #FO_DELETE
    SHFileOp\fFlags = #FOF_SILENT|#FOF_NOCONFIRMATION
    result = SHFileOperation_(SHFileOp)
    FreeMemory(m)
  EndIf  
EndProcedure

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 5:16 pm
by davebar
Thanks Rashad! Works perfectly

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 5:17 pm
by RASHAD
You are welcome :)

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 5:19 pm
by BarryG
davebar wrote:MyFile$ is not deleted and the recycle bin is empty when the execution finishes.
Hmm, I used your code and same file location and the procedure returned 0 (success) for me and recycled the file. Let us know what Rashad's code does.

[Edit] Oops, I see you replied while I was replying. Cool! I'll switch to Rashad's code too, then, to ensure 100% compatibility with all my users computers.

Re: Delete a file to the Windows 10 recycle bin

Posted: Sat Mar 20, 2021 5:24 pm
by davebar
Thanks anyway BarryG, for taking the time to try and help.
Kind Regards
Dave