-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcreatetree.m
131 lines (118 loc) · 2.94 KB
/
createtree.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function [s,z,dirs] = createtree(pathname,ext)
%CREATETREE(PATHNAME,EXT) Finds all files in the input directory.
%
%Input:
%- pathname
%- extention (for instance '.mat' '.dcm'). Optional argument.
%
%Output:
%- s list of fullfilenames as cell array
%- z list of size of each file [OPTIONAL]
%- dirs list of folders [OPTIONAL]
if nargin<2
ext = [];
end
%Handle the case when called with empty extension
if isempty(ext)
ext = '*';
else
ext = ['*',ext];
end
%Extract the list using dir command
f = dir([pathname filesep '**' filesep ext]); %example f = dir('C:/DATA/**/*.dcm') returns dcm files
%Create full path. cat(1,{ffolder} converts to cell array and then use
%strcat to concatenate the strings
if isempty(f)
s = {};
z = [];
return
end
%remove directory pointers
f = f(~[f.isdir]);
if isempty(f)
s = {};
z = [];
return
end
s = strcat(cat(1,{f.folder}),repmat({filesep},1,length(f)),cat(1,{f.name}));
%Extract size
if nargout>1
z = cat(1,f.bytes);
end
%Extract list of folders
if nargout>2
dirs = union(cat(1,{f.folder}),{}); %union sorts and removes duplicates
end
%-----------OLD CODE ---------------
%function [s,z,dirs] = createtree(pathname,s,z,dirs,ext)
%CREATETREE Finds all files in the input directory.
%
%Input:
%- pathname
%- extention (for instance '.mat' '.dcm'). Optional argument.
%Output:
%- cell array of full filenames to files
%- z size of the files.
%- dirs cell array of paths
%Creates a path tree (s), and sizes of files z
%Check for optional calling syntax
% if nargin<5
% if nargin<2
% ext = [];
% else
% ext = s; %Optional calling is createtree(pathname,ext)
% end
% end
%
% if nargin<3
% s = {};
% z = [];
% dirs = {};
% levelone = true;
% else
% levelone = false;
% end
%
% f=dir(pathname);
%
% if levelone
% h = waitbar(0,'Locating files.');
% end
%
% for loop = 1:length(f)
% if f(loop).isdir
% switch f(loop).name
% case '.'
% case '..'
% otherwise
% %disp(sprintf('Parsed Directory %s%s%s.',pathname,filesep,f(loop).name))
% [s,z,dirs] = createtree([pathname filesep f(loop).name],s,z,dirs,ext);
% dirs = [dirs {[pathname filesep f(loop).name]}];
% end
% else
% %disp(sprintf('add %s.',f(loop).name));
% if ~isempty(ext)
% l = length(f(loop).name);
% p = find(f(loop).name=='.');
% if (l>4) && (~isempty(p))
% if isequal(f(loop).name(p(end):l),ext)
% s = [s {[pathname filesep f(loop).name]}];
% z = [z f(loop).bytes];
% end
% end
% else
% %extenstion is empty => add allways
% s = [s {[pathname filesep f(loop).name]}];
% z = [z f(loop).bytes];
% end
% end
%
% if levelone
% h = waitbar(loop/length(f),h);% Changed by Klas to waitbar instead of waitbarupdate.
% end
% end
%
% if levelone
% close(h);
% end
%