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

6 visualizaciones (últimos 30 días)
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 comentarios
Bjorn Gustavsson
Bjorn Gustavsson el 7 de Oct. de 2022
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?).

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 7 de Oct. de 2022
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.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by