sending a file to the recycle bin?

Windows specific forum
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

sending a file to the recycle bin?

Post by jassing »

I just noticed that the old method (shFileOperation) to send files to the recycle bin no longer seems to work.
I was able to get iFileOperation to delete a file, but not recycle it.
Has anyone else already solved this?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: sending a file to the recycle bin?

Post by RASHAD »

Hi

Code: Select all

File$ = "e:\mmedia\pictures\3.jpg"
memory = AllocateMemory( (Len(File$)+2 ) * SizeOf(Character))
SHFileOp.SHFILEOPSTRUCT                
If memory
  PokeS(memory,File$)
  SHFileOp\pFrom  = memory
  SHFileOp\wFunc  = #FO_DELETE
  SHFileOp\fFlags = #FOF_ALLOWUNDO|#FOF_SILENT
  result = SHFileOperation_(SHFileOp)
  FreeMemory(memory)
EndIf
Edit :Modified
Last edited by RASHAD on Mon Nov 19, 2018 5:56 am, edited 1 time in total.
Egypt my love
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: sending a file to the recycle bin?

Post by jassing »

Hi Rashad. Yes, that's basically what I used:
I had a version of that; but it was failing on win10... I'll try yours & try to figure out why mine was failing...

Thanks.
-j
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: sending a file to the recycle bin?

Post by Lunasole »

RASHAD wrote: Mon Nov 19, 2018 3:24 am Hi

Yes this seems still work.
I modified my such example, previously I was using raw arrays of strings or string pointers, but that also didn't works anymore so your variant with memory packing remains.
This is one for multiple files.
I also tried to use IFileOperation interface for this, but too problematic use that from PB.

Code: Select all

; Deletes specified files using Windows Shell
; Files$			a list of files/folders separated using "|"
; RETURN:			#True on success, #False else
Procedure SHDeleteFiles(Files$)
	Protected SrcCount = CountString(Files$, "|")
	Protected *fMem = AllocateMemory(StringByteLength(Files$) + SizeOf(Character) * (SrcCount+2))
	Protected *cMem = *fMem
	Protected fOp.SHFILEOPSTRUCT
	Protected Res
	Protected TFile$
	If *fMem
		While SrcCount >= 0
			TFile$ = StringField(Files$, SrcCount+1, "|")
			If TFile$
				*cMem = *cMem + PokeS(*cMem, TFile$, -1, #PB_String_NoZero) + SizeOf(Character)
			EndIf
			SrcCount - 1
		Wend

		With fOp
			\pFrom = *fMem
			\wFunc = #FO_DELETE
			\fFlags = #FOF_ALLOWUNDO ; + other flags
		EndWith

		Res = SHFileOperation_(fOp)
		FreeMemory(*fMem)
	EndIf
	
	ProcedureReturn Bool(Res = 0)
EndProcedure
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
jacdelad
Addict
Addict
Posts: 1477
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: sending a file to the recycle bin?

Post by jacdelad »

Works nicely. Now here is a modified version which enhances the DeleteFile-function from PureBasic with a #PB_FileSystem_Recycle-flag to recycle the file(s) instead of deleting them (obviously still only for Windows):

Code: Select all

#PB_FileSystem_Recycle = 4096
Procedure DeleteFile_Ext(FileName$,Flags)
  If Flags&#PB_FileSystem_Recycle
    Protected *fMem = AllocateMemory(StringByteLength(FileName$)+2*SizeOf(Character)),fOp.SHFILEOPSTRUCT,Res
    PokeS(*fMem,FileName$)
    With fOp
      \pFrom=*fMem
      \wFunc=#FO_DELETE
      \fFlags=#FOF_ALLOWUNDO|#FOF_SILENT
    EndWith
    Res=SHFileOperation_(fOp)
    FreeMemory(*fMem)
    ProcedureReturn Bool(Res=0)
  Else
    ProcedureReturn DeleteFile(FileName$,Flags)
  EndIf
EndProcedure
Macro DeleteFile(iFile,iFlags)
  DeleteFile_Ext(iFile,iFlags)
EndMacro
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply