MATLAB: How to skip (iteration) empty folders and empty text files

data acquisitionData Acquisition ToolboxdatabaseDatabase ToolboxstatisticsStatistics and Machine Learning Toolbox

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,

Best Answer

%Get list of all files in current directory and beneath
[~,list] = system('dir /S *.txt');
result = textscan(list, '%s', 'delimiter', '\n');
fileList = result{1};
%Remove empty lines
fileList(cellfun(@isempty,fileList)) = [];
%Get full paths of .txt files with size > 0
filesDir = [];
for i = 3:length(fileList)-3
strData = strsplit(fileList{i});
%Get directory name
if (strcmp(strData{1}, 'Directory'))
currDir = strData{3};
elseif (strcmp(strData{2}, 'File(s)'))
%Do nothing
%Save names of files with size > 0
elseif ~(strcmp(strData{3}, '0'))
filesDir = [filesDir; currDir strData{4}];
end
end