Unable to Delete Empty Folders 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:

Unable to Delete Empty Folders to Recycle Bin

Post by IdeasVacuum »

This code works fine with as many files as are thrown at it:

Code: Select all

Global NewList sgFilesToTrash.s()

               AddElement(sgFilesToTrash())
                          sgFilesToTrash() = "C:\MyTemp\Folder03_EMT"

Procedure ToRecycleBin(*ptrFile)
;--------------------------------

sTemp.s = PeekS(*ptrFile,-1,#PB_UTF8)
Debug "ToRecycleBin(*ptrFile)-->" + sTemp + "<--"

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(),0)

*ptrFile = AllocateMemory(StringByteLength(sgFilesToTrash()) + (2 * SizeOf(Character)))
                           PokeS(*ptrFile,sgFilesToTrash(),-1,#PB_UTF8)

Debug "sgFilesToTrash()-->" + sgFilesToTrash() + "<--" ;C:\MyTemp\Folder03_EMT

ToRecycleBin(*ptrFile)
However, if any of the files are (empty) Folders, they do not get deleted to the Recycle Bin.
Error returned: Cannot delete file: Cannot read from the source file or disk.
It's obviously possible to delete folders, so what could be wrong?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
JHPJHP
Addict
Addict
Posts: 2273
Joined: Sat Oct 09, 2010 3:47 am

Re: Unable to Delete Empty Folders to Recycle Bin

Post by JHPJHP »

Windows 7 - 64bit
Windows XP - 32bit

If I understand you correctly:

The folder C:\MyTemp\Folder03_EMT contains some files and an empty folder, and cannot be deleted (due to the empty folder).

I tried the above example, and various other combinations - each one deleted successfully to the Recycle Bin using the posted code.

Probably not any of these... but
Looking beyond the code, this can be experienced if the hard drive has a corruption in that segment - have you tried this from an alternate location. This can also occur if the folder contains a hidden null character, or the rights on the folder don't allow it.

- Try adding a wildcard: "C:\MyTemp\Folder03_EMT*"

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Unable to Delete Empty Folders to Recycle Bin

Post by IdeasVacuum »

Thanks for your advice JHPJHP. "C:\MyTemp\Folder03_EMT" is an empty folder and can be deleted manually, so your tests are encouraging. :)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Unable to Delete Empty Folders to Recycle Bin

Post by IdeasVacuum »

Update: Seems there was something wrong with my install of PB5.20 Beta8, the code always works under PB5.20 Beta10.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply