필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
DZ
DZ 2022년 10월 6일
댓글: Walter Roberson 2022년 10월 7일
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
Bjorn Gustavsson
Bjorn Gustavsson 2022년 10월 7일
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?).
Walter Roberson
Walter Roberson 2022년 10월 7일
https://www.youtube.com/watch?v=xAIQEgYsBRY points out a Windows command line utility that can be used to recycle files.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2022년 10월 7일
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.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by