Page 1 of 1

Question about deleting files and the trash

Posted: Thu Dec 21, 2023 9:34 am
by driessen
Hello,

If I delete a file in my application, of course, the file is gone.
But is there a possibility to delete file in a way that the deleted files can be restored from the trash?
Moving to $Recycle.Bin doesn't seem to be enough.

Thank you.

Re: Question about deleting files and the trash

Posted: Thu Dec 21, 2023 10:02 am
by Antonio Linares
Dear Michel,

It seems as this code has to be used:

Code: Select all | Expand

#include <stdio.h>
#include <windows.h>
#include <shellapi.h>

#define FO_DELETE 3 //Delete the files specified in pFrom.
#define FOF_ALLOWUNDO 0x40 //Preserve undo information, if possible.
#define FOF_NOCONFIRMATION 0x0010 //Do not ask the user to confirm selection.

typedef struct _SHFILEOPSTRUCT {
    HWND hwnd;
    UINT wFunc;
    LPCSTR pFrom;
    LPCSTR pTo;
    FILEOP_FLAGS fFlags;
    BOOL fAnyOperationsAborted;
    LPVOID hNameMappings;
    LPCSTR lpszProgressTitle;
} SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;

int main()
{
    SHFILEOPSTRUCT shf;
    shf.wFunc = FO_DELETE;
    shf.pFrom = "C:\\myfile.txt\0\0"; //File to delete
    shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
    int result = SHFileOperation(&shf);
    if (result == 0)
    {
        printf("File deleted successfully.\n");
    }
    else
    {
        printf("Error: %d\n", result);
    }
    return 0;
}
 
So basically we have to use SHFileOperation() from FWH. We are going to build an example.

Re: Question about deleting files and the trash

Posted: Thu Dec 21, 2023 10:04 am
by Jimmy
hi,

i´m not sure if there is a simple API Call. i use SHFile() Operation like Explorer

when set Flag FOF_ALLOWUNDO it will move deleted Files into Recycle-bin
have a look at https://forums.fivetechsupport.com/view ... hp?t=42417

Re: Question about deleting files and the trash

Posted: Fri Dec 22, 2023 11:00 am
by driessen
Hello Jimmy,

Deleting files by using ShFileOperation() works fine.
But the deleted files are not moved to the recycle bin yet.
So I need to set the flag FOF_ALLOWUNDO.
But how do I do that?

Thanks.

Re: Question about deleting files and the trash

Posted: Fri Dec 22, 2023 11:05 am
by Antonio Linares
Dear Michel,

It is the fifth parameter when you call SHFILEOPERATION()

Re: Question about deleting files and the trash

Posted: Fri Dec 22, 2023 11:18 am
by driessen
Antonio,

I tried that, but there is no chance. No file in the recyle bin.

This is the code I use:

Code: Select all | Expand

#define FO_DELETE         0x0003
#define FOF_ALLOWUNDO     0x0040

******************************************************************************

DeleteFile( DOCDOEL, ArcLst )  // DOCDOEL = file that has to be deleted
                               // ArcLst = Dialog Box

******************************************************************************

static function DeleteFile( cFileName , oDlg )

return SHFileOperation( oDlg:hWnd, FO_DELETE , cFileName + Chr( 0 ) ,, FOF_ALLOWUNDO )
Thanks a lot.

Re: Question about deleting files and the trash

Posted: Fri Dec 22, 2023 12:03 pm
by Antonio Linares
Please do:

MsgInfo( DeleteFile( DOCDOEL, ArcLst ) )

and let us know what you get

Re: Question about deleting files and the trash

Posted: Fri Dec 22, 2023 12:31 pm
by driessen
Antonio,

The answer I get is : 0,00

Re: Question about deleting files and the trash

Posted: Fri Dec 22, 2023 1:27 pm
by Antonio Linares
https://learn.microsoft.com/en-us/windo ... operationa
Returns zero if successful; otherwise nonzero
so it seems as it is properly doing it...

I can't understand why they don't appear at the recycle bin

Re: Question about deleting files and the trash

Posted: Tue Dec 26, 2023 7:42 pm
by jll-fwh
Hello Secarse:

Have you tried moving it to the trash without deleting it? It disappears from the source folder and goes to the trash can.

greeting
Javier Lloris
javierllorisprogramador@gmail.com

Blogs personales:
Programación: https://javierlloris.blogspot.com/
Japonés: https://bitacorajaponesa.blogspot.com/
Cubos de Rubik: https://www.instagram.com/jllorisperson ... 2ybz0gmvul
Tengo una colección de unos 100 cubos de todo tipo de formas geométricas, todos resueltos, en instagram solo hay unos cuentos cubos.

Re: Question about deleting files and the trash

Posted: Wed Jan 03, 2024 8:41 am
by driessen
Javier,

Just moving to the trash, how do I do that?

To everyone, any idea what is going wrong?

Thanks.

Re: Question about deleting files and the trash

Posted: Wed Jan 03, 2024 9:21 am
by Jimmy
hi,

have you try my CODE Sample https://forums.fivetechsupport.com/view ... hp?t=42417
it work with Recylebin

Note : you must use "full-Path"
If pFrom is set to a file name without a full path, deleting the file with FO_DELETE does not move it to the Recycle Bin, even if the FOF_ALLOWUNDO flag is set. You must provide a full path to delete the file to the Recycle Bin.
https://learn.microsoft.com/en-us/windo ... eopstructa