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

Just starting out? Need help? Post your questions and find answers here.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

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

Post 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
Last edited by davebar on Sat Mar 20, 2021 6:12 pm, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4153
Joined: Thu Apr 18, 2019 8:17 am

Re: Delete a file to the Windows 10 recycle bin

Post by BarryG »

Post your code so we can see what's wrong. The other examples work for me.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Delete a file to the Windows 10 recycle bin

Post 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.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Delete a file to the Windows 10 recycle bin

Post 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.
BarryG
Addict
Addict
Posts: 4153
Joined: Thu Apr 18, 2019 8:17 am

Re: Delete a file to the Windows 10 recycle bin

Post 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.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Delete a file to the Windows 10 recycle bin

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4951
Joined: Sun Apr 12, 2009 6:27 am

Re: Delete a file to the Windows 10 recycle bin

Post 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
Egypt my love
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Delete a file to the Windows 10 recycle bin

Post by davebar »

Thanks Rashad! Works perfectly
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4951
Joined: Sun Apr 12, 2009 6:27 am

Re: Delete a file to the Windows 10 recycle bin

Post by RASHAD »

You are welcome :)
Egypt my love
BarryG
Addict
Addict
Posts: 4153
Joined: Thu Apr 18, 2019 8:17 am

Re: Delete a file to the Windows 10 recycle bin

Post 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.
davebar
User
User
Posts: 82
Joined: Fri Aug 31, 2018 9:23 am
Location: Australia

Re: Delete a file to the Windows 10 recycle bin

Post by davebar »

Thanks anyway BarryG, for taking the time to try and help.
Kind Regards
Dave
Post Reply