How to programmatically delete a folder with content to recycle bin as a whole

I am running Matlab on Windows 10.
I want to programmatically recycle some (hundreds of) folders with (thousands of) files inside to recycle bin.
The use case to preserve the files with their folder structure temporarily so that if my other script that manipulates the files and reorganize them into new files/folders throws unexpected warnings, I can get a second chance to check with the original files. Hence the need for selectively restoring folders -- as opposed to individual files. In my use case, the individual files would be untrackable.
The following passage puts individual files to recycle bin.
previousState=recycle('on');
delete(filename);
recycle(previousState);
But replacing delete(filename) with rmdir(DIR,'s') would result in the folder DIR being deleted permanently.
The following puts individual files to recycle bin. But loses the folder structure.
delete(fullfile(DIR,'*')
Hence,
Question:
Is there a way to programmatically delete a folder with content to recycle bin as a whole?

3 Comments

The structure isn't in the Recycle Bin on Windows, but it stores the original file location so retrieving it puts it back where it was. I don't use it enough to know for sure, but I presume it will recreate a missing directory if the file(s) from it are retrieved.
I've also not tried, can you simply move/copy the files there by movefile? That would preserve the directory structure.
QD workaround suggestion: If you make a parallel directory structure and shift the copies of everything into that after processing/renaming/moving the files in the script, then you'll have the pre-your-script versions saved away in an organized manner ready to be bulk-removed once you've verified that everything when as planned. The files will not take more space in such a back-up-fail-safe directory-tree than in the recycle-bin (I think?).
https://www.youtube.com/watch?v=xAIQEgYsBRY points out a Windows command line utility that can be used to recycle files.

Sign in to comment.

Answers (1)

Using another folder instead of the recycle bin has severe advantages:
  • The recycle bin fails, if the path name of the deleted file has more than 260 characters.
  • If the recycle bin is full, older files are deleted.
  • If the size of the folder tree exceeds the size of the recycle bin, the files are deleted.
  • There might be no recycle bin on network drives, USB sticks or removaböe media.
These problems will occur, if you move the folder tree manually to the recycle bin later. But then you see a corresponding message at least.
You can move a folder tree to the recycling bin using a C-Mex function an the Windows API. I've written some code to do this and decided not to publish it due to the mentioned problems. The risk is too high to loose important data, if the user trust the assumption, that the data are stored safely in the recycling bin.
So my advice: Don't use the recycle bin, but a specific folder to store the data intermediately. This is cheap, because renaming the base folder is enough.

Categories

Products

Release

R2020b

Asked:

DZ
on 6 Oct 2022

Commented:

on 7 Oct 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!