[Done] Unable to Delete to Recycle Bin

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

[Done] Unable to Delete to Recycle Bin

Post by IdeasVacuum »

Well, I can - and I can't.

WindowsXP x86, PB 5.20 Beta8. Compiled as Unicode.

Code: Select all

Global NewList sgToTrash.s()
               AddElement(sgToTrash())
                          sgToTrash() = "C:\MyTemp\Folder02\Text Document_202.txt"

Global NewList sgFilesToTrash.s()

               AddElement(sgFilesToTrash())
                          sgFilesToTrash() = "C:\MyTemp\Folder02\Text Document_202.txt"
               AddElement(sgFilesToTrash())
                          sgFilesToTrash() = "C:\MyTemp\Folder02\Text Document_204.txt"

Procedure ToRecycleBin(sFile.s)
;------------------------------
Protected SHFileOp.SHFILEOPSTRUCT
          SHFileOp\pFrom = @sFile
          SHFileOp\wFunc = #FO_DELETE
          SHFileOp\fFlags = #FOF_ALLOWUNDO | #FOF_NOCONFIRMATION ;| #FOF_SILENT

               If SHFileOperation_(@SHFileOp) = 0

                       ProcedureReturn #True
               Else
                       ProcedureReturn #False
               EndIf
EndProcedure

;==== Fails using SelectElement ===================================================================
 FirstElement(sgFilesToTrash())
SelectElement(sgFilesToTrash(),1)

Debug "sgFilesToTrash()-->" + sgFilesToTrash() + "<--" ;C:\MyTemp\Folder02\Text Document_204.txt

ToRecycleBin(sgFilesToTrash())
;Error returned: Cannot delete file: Cannot read from the source file or disk.
;==========================================================================================

;==== Fails using FirstElement ====================================================================
 FirstElement(sgFilesToTrash())

Debug "sgFilesToTrash()-->" + sgFilesToTrash() + "<--" ;C:\MyTemp\Folder02\Text Document_202.txt

ToRecycleBin(sgFilesToTrash())
;Error returned: Cannot delete file: Cannot read from the source file or disk.
;==========================================================================================

;==== Works if only one Element exists =============================================================
Debug "sgToTrash()-->" + sgToTrash() + "<--" ;C:\MyTemp\Folder02\Text Document_202.txt
ToRecycleBin(sgToTrash())
;==========================================================================================

;==== Works Explicitly ==========================================================================

ToRecycleBin("C:\MyTemp\Folder02\Text Document_204.txt")
;==========================================================================================
So, where am I going wrong? I have tried imposing speech marks, still no joy.

Edit: See solution in my post below
Last edited by IdeasVacuum on Mon Aug 12, 2013 12:25 am, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
idle
Always Here
Always Here
Posts: 6048
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Unable to Delete to Recycle Bin

Post by idle »

maybe it wants an ascii string in the structure field
Windows 11, Manjaro, Raspberry Pi OS
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Unable to Delete to Recycle Bin

Post by IdeasVacuum »

Hi Idle, turns out the string has to be double null terminated. So, this works:

Code: Select all

Global NewList sgFilesToTrash.s()

               AddElement(sgFilesToTrash())
                          sgFilesToTrash() = "C:\MyTemp\Folder02\Text Document_202.txt"
               AddElement(sgFilesToTrash())
                          sgFilesToTrash() = "C:\MyTemp\Folder02\Text Document_204.txt"

Procedure ToRecycleBin(*ptrFile)
;------------------------------
Protected SHFileOp.SHFILEOPSTRUCT
          SHFileOp\pFrom = *ptrFile
          SHFileOp\pTo = #Null
          SHFileOp\wFunc = #FO_DELETE
          SHFileOp\fFlags = #FOF_ALLOWUNDO | #FOF_NOCONFIRMATION ;| #FOF_SILENT

               If SHFileOperation_(@SHFileOp) = 0

                       ProcedureReturn #True
               Else
                       ProcedureReturn #False
               EndIf
EndProcedure

SelectElement(sgFilesToTrash(),1)

*ptrFile = AllocateMemory(StringByteLength(sgFilesToTrash()) + 2)
                    PokeS(*ptrFile,sgFilesToTrash())

Debug "sgFilesToTrash()-->" + sgFilesToTrash() + "<--" ;C:\MyTemp\Folder02\Text Document_204.txt

ToRecycleBin(*ptrFile)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply