Page 1 of 1

sending a file to the recycle bin?

Posted: Sun Nov 18, 2018 11:45 pm
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?

Re: sending a file to the recycle bin?

Posted: Mon Nov 19, 2018 3:24 am
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

Re: sending a file to the recycle bin?

Posted: Mon Nov 19, 2018 5:48 am
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

Re: sending a file to the recycle bin?

Posted: Sun Apr 02, 2023 4:27 pm
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

Re: sending a file to the recycle bin?

Posted: Tue Apr 04, 2023 1:07 am
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