필터 지우기
필터 지우기

Removing an empty folder and subfolder?

조회 수: 14 (최근 30일)
Joey Cavale
Joey Cavale 2017년 7월 20일
댓글: Will 2022년 12월 6일
I have a code that searches through multiple subfolders and deletes all files with a .EIA extension. I want to know if there is any way I can search through the same subfolders and remove the ones that no longer have any files in them.

채택된 답변

dpb
dpb 2017년 7월 20일
The dir structure on those subdirectories will return only the two directories entries '.' and '..'. So, check length(d)==2 and the two d.name match the above is certain; I don't think there's a way two get two entries only and they not be anything but the above so just the length check would be all needed.
Of course, if there's anything otherwise left then the rmdir command will fail so the even simpler way is to just issue it in a try...catch block and those that are empty will disappear almost automagically! :)
  댓글 수: 3
dpb
dpb 2017년 7월 21일
Sometimes things are just too easy to see... :) This "trick" dawned on me sometime back looking at the same issue (not with Matlab at the time, though).
Will
Will 2022년 12월 6일
You can just call rmdir without the try...catch block. If you return the second output which contains error or warning messages it will fail silently wihout output to the terminal.
mkdir('notempty')
fclose(fopen('notempty/test.file', 'w'));
mkdir('empty')
[~, ~] = rmdir('notempty');
[~, ~] = rmdir('empty');
dir('.')
. .. notempty

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by