-
Notifications
You must be signed in to change notification settings - Fork 6
/
ReadAllFiles.m
48 lines (34 loc) · 1.09 KB
/
ReadAllFiles.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function [files] = ReadAllFiles(folderPath, ext)
% This method returns all dcm files to search all folders
%
% input :
% folderPath = The path of folder that located files to find
% ext = The extension of file to find
dirData = dir(folderPath);
dirIdx = [dirData.isdir];
files = [];
fileList = {dirData(~dirIdx).name}';
if ~isempty(fileList)
files = GetFiles(fileList, ext);
files = cellfun(@(x) fullfile(folderPath,x),...
files, 'UniformOutput', false);
end
subDirs = {dirData(dirIdx).name};
validIdx = ~ismember(subDirs, {'.', '..'});
for iDir = find(validIdx)
nextDir = fullfile(folderPath, subDirs{iDir});
files = [files; GetAllFiles(nextDir, ext)];
end
% -----------------------------------------------------------------------
function [files] = GetFiles(files, ext)
count = length(files);
idx = zeros(count,1);
for i=1:count
[~,~,e] = fileparts(files{i});
if isequal(e, strcat('.', ext))
idx(i) = 1;
else
idx(i) = 0;
end
end
files = files(idx~=0);