How to skip (iteration) empty folders and empty text files
Show older comments
Hi,
I have main folder, and many sub-folders. Inside each sub-folders, there are many sub-folders(sub-sub-folders). The sub-sub-folders contain many files(mainly text). I want to only read text files. But unfortunately some sub-folders are empty or some sub-sub-folders contain other files (non-text files). So, I wish the following:
1. Skip if the sub-folder is empty
2. Skip if the text file is empty
My meaning: skip means skip the iteration. I am reading the text files as following:
by each sub-folder and by each sub-sub-folders (the text files)
Please some one help me how to do this.
Many many thanks in advance,
1 Comment
KSSV
on 26 Oct 2016
doc isempty
Accepted Answer
More Answers (1)
USe one of the many recursive dir commands from the FileExchange: http://de.mathworks.com/matlabcentral/fileexchange/?utf8=%E2%9C%93&term=dir+recursive, e.g. http://de.mathworks.com/matlabcentral/fileexchange/15505-recursive-dir:
FileList = dirrec(MainFolder, '*.txt');
for k = 1:numel(FileList)
FileInfo = dir(FileList{k});
if FileInfo.bytes > 0
... import the data ...
end
end
Some of the recursive dir versions reply a struc like Matlab's dir(), then:
DirList = <the_dir_replacement>(MainFolder, '*.txt');
DirList = DirList([DirList.bytes] ~= 0);
for k = 1:numel(FileList)
FileName = DirList(k).name;
... import the data...
end
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!