-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdirc.m
396 lines (299 loc) · 11.3 KB
/
dirc.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
function[list]=dirc(dir_name,filter,sort_by,group)
%DIRC Directory listing with cell output and optional arguments.
%
% DIRC(DIR_NAME) uses the DIR function to return the directory listing of
% directory DIR_NAME, with usage of optional arguments, and with the
% following differences in output:
%
% - The output is in a 'cell' format instead of a 'structure' format.
%
% - The output includes the following columns (in the stated order):
% Full Item Name, Item Name, Item Extension, Date, Bytes, IsDir
%
% OPTIONAL ARGUMENTS:
%
% The following are arguments that can optionally be used:
%
% (If the value for any of the arguments below is left blank, i.e. if it
% is entered as '', the stated default value is used.)
%
% FILTER:
% a: Return all items (both files and directories) (default).
% ae: Return all items (both files and directories), but excludes the
% '.' and '..' directories if they are present.
% f: Return only files.
% d: Return only directories.
% de: Return only directories, and excludes the '.' and '..'
% directories if they are present.
%
% SORT_BY:
% o: Use original DIR function's dictionary sort (sort by full name)
% (case-sensitive) (default).
% n: Sort by full name (case-insensitive) (alphabetic).
% e: Sort by extension (alphabetic).
% t: Sort by extension and where equal, sort further by full name
% (case-insensitive) (alphabetic).
% d: Sort by date and time (oldest first).
% s: Sort by size (smallest first).
% r: (Suffix this to any of the above, to sort in reverse order.)
%
% GROUP:
% n: Do not group (default).
% d: Group directories first.
% f: Group files first.
%
% EXAMPLES:
% dirc('C:\')
% dirc('C:\Windows','de','n')
% dirc('C:\','','t','d')
%
% REMARKS:
%
% Because FILEPARTS is used to parse the FULL ITEM NAME into ITEM NAME
% and ITEM EXTENSION (as long as the item is a file), the function uses
% additional time to run (assuming PARSE_ITEM_NAMES under CONFIGURABLE
% OPTIONS is left enabled).
%
% The ITEM EXTENSION, if present, does not include an initial dot. This
% is unlike the output of the FILEPARTS function, where the ITEM
% EXTENSION of the output does include an initial dot.
%
% See the CONFIGURABLE OPTIONS section in the code for additional
% options.
%
% VERSION DATE: 2005.06.10
% MATLAB VERSION: 7.0.1.24704 (R14) Service Pack 1
%
% See also DIR, FILEPARTS.
%{
REVISION HISTORY:
2005.06.10: Added 'or' SORT_BY option.
2005.04.12: Removed undesirable 'clc' line from code.
2005.04.07: Original release.
KEYWORDS:
dir, directory, directories, directory listing, folder, folders,
file, files, file name, file names, filename, filenames, fileparts,
ext, extension
%}
%**************************************************************************
%% CONFIGURABLE OPTIONS
parse_item_names=1;
%If set to 1 (default), items are parsed into name and extension. If set to
%0, items are not parsed into name and extension, and those two columns in
%the output are left empty.
parse_dir_names=0;
%If set to 0 (default), only files are parsed into name and extension, and
%directories are not. If set to 1, both files and directories are parsed to
%name and extension. This applies only if PARSE_ITEM_NAMES is set to 1.
%**************************************************************************
%% Confirm FILTER value, if entered, is valid; else set default value.
if nargin>=2,
switch filter
case{'','a','ae','f','d','de'}
otherwise
error('Invalid filter option.')
end
else
filter='';
end
%--------------------------------------------------------------------------
%% Confirm SORT_BY value, if entered, is valid; else set default value.
if nargin>=3,
switch sort_by
case{'','o','or','n','nr','e','er','t','tr','d','dr','s','sr'}
otherwise
error('Invalid sort option.')
end
else
sort_by='';
end
%--------------------------------------------------------------------------
%% Confirm GROUP value, if entered, is valid; else set default value.
if nargin>=4,
switch group
case{'','n','d','f'}
otherwise
error('Invalid group option.')
end
else
group='';
end
%**************************************************************************
%% Get directory listing and convert it to cell format.
list=dir(dir_name);
list=struct2cell(list);
list=list';
%--------------------------------------------------------------------------
%% Insert columns for ITEM NAME and ITEM EXTENSION.
list(:,7:9)=list(:,2:4);
list(:,2:4)='';
%--------------------------------------------------------------------------
%% If PARSE_ITEM_NAMES is enabled, parse and store FULL ITEM NAME into ITEM
% NAME and ITEM EXTENSION in LIST.
if parse_item_names
list_items=size(list,1);
for item_count=1:list_items
%Query ITEM ISDIR status.
item_isdir=list(item_count,6);
item_isdir=cell2mat(item_isdir);
%If ITEM is file or PARSE_DIR_NAMES is enabled, update LIST with
%ITEM_NAME and ITEM_EXT.
item_isfile=(item_isdir==0);
if item_isfile || parse_dir_names
%Query FULL_ITEM_NAME.
full_item_name=list(item_count,1);
full_item_name=cell2mat(full_item_name);
%Generate ITEM_NAME and ITEM_EXT.
[item_path,item_name,item_ext]=...
fileparts([dir_name,'\',full_item_name]);
%IF ITEM_EXT exists, remove dot from it.
item_ext_size=numel(item_ext);
item_ext_exists=(item_ext_size>0);
if item_ext_exists
item_ext=item_ext(2:end);
end
%Update LIST with ITEM_NAME and ITEM_EXT.
list(item_count,2)={item_name};
list(item_count,3)={item_ext};
end
end
end
%**************************************************************************
%% Filter LIST as relevant.
switch filter
case{'','a'}
%Do not delete anything.
case{'ae'}
%Delete '.' and '..' directories if they exist.
if strcmp(cell2mat(list(1,1)),'.') && ...
strcmp(cell2mat(list(2,1)),'..')
list(1:2,:)=[];
end
case{'f'}
%Determine directory indices.
isdir_values=list(:,6);
isdir_values=cell2mat(isdir_values);
dir_indices=find(isdir_values==1);
%Delete directories.
list(dir_indices,:)=[];
case{'d'}
%Determine files.
isdir_values=list(:,6);
isdir_values=cell2mat(isdir_values);
file_indices=find(isdir_values==0);
%Delete files.
list(file_indices,:)=[];
case{'de'}
%Determine file indices.
isdir_values=list(:,6);
isdir_values=cell2mat(isdir_values);
file_indices=find(isdir_values==0);
%Delete files.
list(file_indices,:)=[];
%Delete '.' and '..' directories if they exist.
if strcmp(cell2mat(list(1,1)),'.') && ...
strcmp(cell2mat(list(2,1)),'..')
list(1:2,:)=[];
end
end
%**************************************************************************
%% Sort LIST as relevant.
switch sort_by
case{'','o','or'}
%Use default sort.
case{'n','nr'}
%Sort by full name.
full_item_names=list(:,1);
full_item_names=lower(full_item_names);
[full_item_names,index]=sortrows(full_item_names);
list=list(index,:);
case{'e','er'}
%Sort by extension
item_exts=list(:,3);
item_exts_numel=size(item_exts,1);
for item_count=1:item_exts_numel
item_ext=item_exts(item_count);
item_ext=cell2mat(item_ext);
if isequal(item_ext,[])
item_exts(item_count)={''};
end
end
item_exts=lower(item_exts);
[item_exts,index]=sortrows(item_exts);
list=list(index,:);
case{'t','tr'}
%Sort by extension and where equal, sort further by full name
%(executes in reverse).
%Sort by full name.
full_item_names=list(:,1);
full_item_names=lower(full_item_names);
[full_item_names,index]=sortrows(full_item_names);
list=list(index,:);
item_exts=list(:,3);
item_exts_numel=size(item_exts,1);
for item_count=1:item_exts_numel
item_ext=item_exts(item_count);
item_ext=cell2mat(item_ext);
if isequal(item_ext,[])
item_exts(item_count)={''};
end
%Sort by extension.
end
item_exts=lower(item_exts);
[item_exts,index]=sortrows(item_exts);
list=list(index,:);
case{'d','dr'}
%Sort by date and time.
item_dates=list(:,4);
item_dates=cell2mat(item_dates);
item_dates=datenum(item_dates);
[item_dates,index]=sortrows(item_dates);
list=list(index,:);
case{'s','sr'}
%Sort by size
item_sizes=list(:,5);
item_sizes=cell2mat(item_sizes);
[item_sizes,index]=sortrows(item_sizes);
list=list(index,:);
end
%--------------------------------------------------------------------------
%% Reverse the sorted order if relevant.
if numel(sort_by)==2 && sort_by(2)=='r'
list_items=size(list,1);
list=list(list_items:-1:1,:);
end
%**************************************************************************
%% Group LIST as relevant.
switch group
case{'','n'}
%Do not group.
case{'','d'}
%Group directories first.
%Determine directory indices.
isdir_values=list(:,6);
isdir_values=cell2mat(isdir_values);
dir_indices=find(isdir_values==1);
%Determine file indices.
isdir_values=list(:,6);
isdir_values=cell2mat(isdir_values);
file_indices=find(isdir_values==0);
%Merge grouping indices.
regrouping_indices=[dir_indices;file_indices];
%Group
list=list(regrouping_indices,:);
case{'','f'}
%Group files first.
%Determine directory indices.
isdir_values=list(:,6);
isdir_values=cell2mat(isdir_values);
dir_indices=find(isdir_values==1);
%Determine file indices.
isdir_values=list(:,6);
isdir_values=cell2mat(isdir_values);
file_indices=find(isdir_values==0);
%Merge grouping indices.
regrouping_indices=[file_indices;dir_indices];
%Group
list=list(regrouping_indices,:);
end
%**************************************************************************