Home > Blockchain >  Move a directory to trash
Move a directory to trash

Time:10-19

is it possible to move a entire directory to trash (like a file QFile::moveToTrash)? There is a folder with a lot of files, which would make it confusing when moving each individual file to the trash.

CodePudding user response:

What you are probably looking for is QDir::removeRecursively.

Removes the directory, including all its contents.

Returns true if successful, otherwise false.

Keep in mind that this does only report wether the operation was successful or not. Deleting the content separately would give you the opportunity to show a progress dialog to the user and report errors more specific.

CodePudding user response:

It is very easy. Just use QFile::moveToTrash(path_to_dir); to your directory, the same way as you would do it with a file. This one line will move the whole directory with all its contents to trash. This works with Qt 5.15.2. There were some bugs in some older versions of Qt (mostly on Linux) so make sure you have the most recent version possible.

The currently accepted answer by Farshid616 is very very wrong. The location of recycle bin can be very different. And can be different for different drives.

Beware however that some drives (flash disks) usually do not support trash bin and the content will be deleted permanently.

  • Related