Page 1 of 1

[Done] Unable to Delete to Recycle Bin

Posted: Sun Aug 11, 2013 10:49 pm
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

Re: Unable to Delete to Recycle Bin

Posted: Sun Aug 11, 2013 11:08 pm
by idle
maybe it wants an ascii string in the structure field

Re: Unable to Delete to Recycle Bin

Posted: Mon Aug 12, 2013 12:15 am
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)