Results 1 to 2 of 2

Thread: Move file to trash on Windows

  1. #1

    Default Move file to trash on Windows

    Hi guys,

    This is my first time trying to implement something using the Windows API... I want to send a file to Trash on Windows. I searched the internet and found a couple more or less helpful pages, and I eventually ended up with the following couple lines of code:

    Qt Code:
    1. QString filepath = "C:\\some\\file";
    2.  
    3. QFile f(filepath);
    4. if(!f.exists())
    5. qDebug() << "ERROR: File doesn't exist";
    6.  
    7. SHFILEOPSTRUCT FileOPStruct;
    8.  
    9. filepath += '\0';
    10.  
    11. FileOPStruct.hwnd=NULL;
    12. FileOPStruct.wFunc=FO_DELETE;
    13. FileOPStruct.pFrom=LPCWSTR(filepath.toStdString().c_str());
    14. FileOPStruct.pTo=NULL;
    15. FileOPStruct.fFlags=FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_ALLOWUNDO | FOF_SILENT;
    16. FileOPStruct.hNameMappings=NULL;
    17. FileOPStruct.lpszProgressTitle=NULL;
    18.  
    19. if(SHFileOperation(&FileOPStruct))
    20. qDebug() << "ERROR: Deletion failed";
    21. else
    22. qDebug() << "Deleted :)";
    To copy to clipboard, switch view to plain text mode 

    The file in QString filepath exists (the check passes just fine), but I always get to "ERROR: Deletion failed", and the file (obviously) is still there. I don't have any previous experience of using the Win API and am just not able to resolve the issue... would anyone mind helping me?

    Thanks a million!! :-)

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,311
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Move file to trash on Windows

    From a Microsoft Web page (Google is your friend):

    pFrom

    Type: PCZZTSTR

    Note This string must be double-null terminated.

    A pointer to one or more source file names. These names should be fully qualified paths to prevent unexpected results.

    Standard MS-DOS wildcard characters, such as "*", are permitted only in the file-name position. Using a wildcard character elsewhere in the string will lead to unpredictable results.

    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 in line 13, you are simply pointing pFrom at a string that terminates in a *single* NULL. Not only that, put you are using the QString::toStdString().c_str() (which returns a char * pointer) methods instead of QString::toStdWString().c_str() (which returns w_char *), and then mangling that by casting it to a LPCWSTR. So who knows what the Windows call is seeing when it actually gets called?

    You need to create a wchar_t array that is at least two characters longer than the length of your string, do a wide string copy of the QString::toStdWString().c_str() into it, then append an additional '\0' onto the end of that. That wchar_t array is what you give to pFrom before calling the shell method.

Similar Threads

  1. Send file / folder to trash
    By thru in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2011, 15:42
  2. How to find out what move file types can be played?
    By Markus in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2010, 22:10
  3. Replies: 1
    Last Post: 28th May 2010, 11:33
  4. Is there a move file to function ?
    By _SamSoft_ in forum Qt Programming
    Replies: 2
    Last Post: 4th June 2007, 19:32
  5. Replies: 3
    Last Post: 25th May 2007, 07:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.