Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot delete files to Recycle Bin

Tags:

delphi

I cannot delete files to Recycle Bin.

VAR SHFileOpStruct: TSHFileOpStruct;
begin   
  with SHFileOpStruct  do
  begin
    wnd   := Handle; 
    wFunc := FO_DELETE;
    pFrom := PChar(FileName);
    fFlags:= 0;
    pTo   := NIL;
    hNameMappings:= NIL;
    lpszProgressTitle:= NIL;
  end;
  Result:= SHFileOperation(SHFileOpStruct); 
end;

I can delete files in this format: '1.xyz' but not in this format '12.xyz' (file name is longer than 1 character).

like image 533
Server Overflow Avatar asked Jun 13 '11 15:06

Server Overflow


People also ask

Why can't I delete files in Recycle Bin?

The disk is write-protected or full; The disk is corrupted; The Recycle Bin is corrupted or full; Malware or virus infection of the file/folder.

How do you force delete a file that won't delete?

Use Shift + Delete to Force Delete File/Folder. You can select the target file or folder and press Shift + Delete keyboard shortcut to delete the file/folder permanently. This file deletion method won't pass the Recycle Bin.

How do I enable Recycle Bin to delete?

Here's how to get the Recycle Bin on your desktop in Windows 10: Select the Start  button, then select Settings . Select Personalization > Themes > Desktop icon settings. Select the RecycleBin check box > Apply.


1 Answers

According to the documentation of SHFileOperation you should not use GetLastError to see if the operation succeeds. Check the Result of the function and use the documentation to figure out the error it returns. That should give you a better clue what the problem is.

EDIT:

Best guess from reading the documentation:

pFrom

Although this member is declared as a single null-terminated string, it is actually a buffer that can hold multiple null-delimited file names. Each file name is terminated by a single NULL character. The last file name is terminated with a double NULL character ("\0\0") to indicate the end of the buffer

So you should make sure pFrom is ended with a double 0. Try the following

pFrom := PChar(FileName + #0);

Also, what Delphi version are you using?

EDIT2:

Also make sure the structure is properly initialized to 0. Uncomment the FillChar

like image 176
Lars Truijens Avatar answered Oct 19 '22 15:10

Lars Truijens