NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/dirc.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 function[list]=dirc(dir_name,filter,sort_by,group)
2
3 %DIRC Directory listing with cell output and optional arguments.
4 %
5 %  DIRC(DIR_NAME) uses the DIR function to return the directory listing of
6 %  directory DIR_NAME, with usage of optional arguments, and with the
7 %  following differences in output:
8 %
9 %    - The output is in a 'cell' format instead of a 'structure' format.
10 %
11 %    - The output includes the following columns (in the stated order):
12 %        Full Item Name, Item Name, Item Extension, Date, Bytes, IsDir
13 %
14 %  OPTIONAL ARGUMENTS:
15 %
16 %    The following are arguments that can optionally be used:
17 %
18 %    (If the value for any of the arguments below is left blank, i.e. if it
19 %    is entered as '', the stated default value is used.)
20 %
21 %    FILTER:
22 %      a:  Return all items (both files and directories) (default).
23 %      ae: Return all items (both files and directories), but excludes the
24 %          '.' and '..' directories if they are present.
25 %      f:  Return only files.
26 %      d:  Return only directories.
27 %      de: Return only directories, and excludes the '.' and '..'
28 %          directories if they are present.
29 %
30 %    SORT_BY:
31 %      o: Use original DIR function's dictionary sort (sort by full name)
32 %         (case-sensitive) (default).
33 %      n: Sort by full name (case-insensitive) (alphabetic).
34 %      e: Sort by extension (alphabetic).
35 %      t: Sort by extension and where equal, sort further by full name
36 %         (case-insensitive) (alphabetic).
37 %      d: Sort by date and time (oldest first).
38 %      s: Sort by size (smallest first).
39 %      r: (Suffix this to any of the above, to sort in reverse order.)
40 %
41 %    GROUP:
42 %      n: Do not group (default).
43 %      d: Group directories first.
44 %      f: Group files first.
45 %
46 %  EXAMPLES:
47 %    dirc('C:\')
48 %    dirc('C:\Windows','de','n')
49 %    dirc('C:\','','t','d')
50 %
51 %  REMARKS:
52 %
53 %    Because FILEPARTS is used to parse the FULL ITEM NAME into ITEM NAME
54 %    and ITEM EXTENSION (as long as the item is a file), the function uses
55 %    additional time to run (assuming PARSE_ITEM_NAMES under CONFIGURABLE
56 %    OPTIONS is left enabled).
57 %
58 %    The ITEM EXTENSION, if present, does not include an initial dot. This
59 %    is unlike the output of the FILEPARTS function, where the ITEM
60 %    EXTENSION of the output does include an initial dot.
61 %
62 %    See the CONFIGURABLE OPTIONS section in the code for additional
63 %    options.
64 %
65 %  VERSION DATE: 2005.06.10
66 %  MATLAB VERSION: 7.0.1.24704 (R14) Service Pack 1
67 %
68 %  See also DIR, FILEPARTS.
69
70 %{
71 %REVISION HISTORY:
72 %2005.06.10: Added 'or' SORT_BY option.
73 %2005.04.12: Removed undesirable 'clc' line from code.
74 %2005.04.07: Original release.
75
76 %KEYWORDS:
77 %dir, directory, directories, directory listing, folder, folders,
78 %file, files, file name, file names, filename, filenames, fileparts,
79 %ext, extension
80 %}
81
82 %**************************************************************************
83
84 %% CONFIGURABLE OPTIONS
85
86 parse_item_names=1;
87 %If set to 1 (default), items are parsed into name and extension. If set to
88 %0, items are not parsed into name and extension, and those two columns in
89 %the output are left empty.
90
91 parse_dir_names=0;
92 %If set to 0 (default), only files are parsed into name and extension, and
93 %directories are not. If set to 1, both files and directories are parsed to
94 %name and extension. This applies only if PARSE_ITEM_NAMES is set to 1.
95
96 %**************************************************************************
97
98 %% Confirm FILTER value, if entered, is valid; else set default value.
99
100 if nargin>=2,
101     switch filter
102         case{'','a','ae','f','d','de'}
103         otherwise
104             error('Invalid filter option.')
105     end
106 else
107     filter='';
108 end
109
110 %--------------------------------------------------------------------------
111
112 %% Confirm SORT_BY value, if entered, is valid; else set default value.
113
114 if nargin>=3,
115     switch sort_by
116         case{'','o','or','n','nr','e','er','t','tr','d','dr','s','sr'}
117         otherwise
118             error('Invalid sort option.')
119     end
120 else
121     sort_by='';
122 end
123
124 %--------------------------------------------------------------------------
125
126 %% Confirm GROUP value, if entered, is valid; else set default value.
127
128 if nargin>=4,
129     switch group
130         case{'','n','d','f'}
131         otherwise
132             error('Invalid group option.')
133     end
134 else
135     group='';
136 end
137
138 %**************************************************************************
139
140 %% Get directory listing and convert it to cell format.
141
142 list=dir(dir_name);
143 list=struct2cell(list);
144 list=list';
145
146 %--------------------------------------------------------------------------
147
148 %% Insert columns for ITEM NAME and ITEM EXTENSION.
149
150 list(:,7:9)=list(:,2:4);
151 list(:,2:4)='';
152
153 %--------------------------------------------------------------------------
154
155 %% If PARSE_ITEM_NAMES is enabled, parse and store FULL ITEM NAME into ITEM
156 %  NAME and ITEM EXTENSION in LIST.
157
158 if parse_item_names
159
160     list_items=size(list,1);
161     for item_count=1:list_items
162
163         %Query ITEM ISDIR status.
164         item_isdir=list(item_count,6);
165         item_isdir=cell2mat(item_isdir);
166
167         %If ITEM is file or PARSE_DIR_NAMES is enabled, update LIST with
168         %ITEM_NAME and ITEM_EXT.
169         item_isfile=(item_isdir==0);
170         if item_isfile || parse_dir_names
171
172             %Query FULL_ITEM_NAME.
173             full_item_name=list(item_count,1);
174             full_item_name=cell2mat(full_item_name);
175
176             %Generate ITEM_NAME and ITEM_EXT.
177             [item_path,item_name,item_ext]=...
178                 fileparts([dir_name,'\',full_item_name]);
179
180             %IF ITEM_EXT exists, remove dot from it.
181             item_ext_size=numel(item_ext);
182             item_ext_exists=(item_ext_size>0);
183             if item_ext_exists
184                 item_ext=item_ext(2:end);
185             end
186
187             %Update LIST with ITEM_NAME and ITEM_EXT.
188             list(item_count,2)={item_name};
189             list(item_count,3)={item_ext};
190
191         end
192
193     end
194
195 end
196
197 %**************************************************************************
198
199 %% Filter LIST as relevant.
200
201 switch filter
202    
203     case{'','a'}
204        
205         %Do not delete anything.
206        
207     case{'ae'}
208        
209         %Delete '.' and '..' directories if they exist.
210         if strcmp(cell2mat(list(1,1)),'.') && ...
211            strcmp(cell2mat(list(2,1)),'..')
212             list(1:2,:)=[];
213         end
214        
215     case{'f'}
216        
217         %Determine directory indices.
218         isdir_values=list(:,6);
219         isdir_values=cell2mat(isdir_values);
220         dir_indices=find(isdir_values==1);
221        
222         %Delete directories.
223         list(dir_indices,:)=[];
224        
225     case{'d'}
226        
227         %Determine files.
228         isdir_values=list(:,6);
229         isdir_values=cell2mat(isdir_values);
230         file_indices=find(isdir_values==0);
231        
232         %Delete files.
233         list(file_indices,:)=[];
234        
235     case{'de'}
236        
237         %Determine file indices.
238         isdir_values=list(:,6);
239         isdir_values=cell2mat(isdir_values);
240         file_indices=find(isdir_values==0);
241        
242         %Delete files.
243         list(file_indices,:)=[];
244        
245         %Delete '.' and '..' directories if they exist.
246         if strcmp(cell2mat(list(1,1)),'.') && ...
247            strcmp(cell2mat(list(2,1)),'..')
248             list(1:2,:)=[];
249         end
250        
251 end
252
253 %**************************************************************************
254
255 %% Sort LIST as relevant.
256
257 switch sort_by
258    
259     case{'','o','or'}
260        
261         %Use default sort.
262        
263     case{'n','nr'}
264        
265         %Sort by full name.
266         full_item_names=list(:,1);
267         full_item_names=lower(full_item_names);
268         [full_item_names,index]=sortrows(full_item_names);
269         list=list(index,:);
270        
271     case{'e','er'}
272        
273         %Sort by extension
274         item_exts=list(:,3);
275         item_exts_numel=size(item_exts,1);
276         for item_count=1:item_exts_numel
277            
278             item_ext=item_exts(item_count);
279             item_ext=cell2mat(item_ext);
280             if isequal(item_ext,[])
281                 item_exts(item_count)={''};
282             end
283            
284         end
285         item_exts=lower(item_exts);
286         [item_exts,index]=sortrows(item_exts);
287         list=list(index,:);
288        
289     case{'t','tr'}
290        
291         %Sort by extension and where equal, sort further by full name
292         %(executes in reverse).
293        
294         %Sort by full name.
295         full_item_names=list(:,1);
296         full_item_names=lower(full_item_names);
297         [full_item_names,index]=sortrows(full_item_names);
298         list=list(index,:);
299        
300         item_exts=list(:,3);
301         item_exts_numel=size(item_exts,1);
302         for item_count=1:item_exts_numel
303            
304             item_ext=item_exts(item_count);
305             item_ext=cell2mat(item_ext);
306             if isequal(item_ext,[])
307                 item_exts(item_count)={''};
308             end
309        
310         %Sort by extension.
311         end
312         item_exts=lower(item_exts);
313         [item_exts,index]=sortrows(item_exts);
314         list=list(index,:);
315        
316     case{'d','dr'}
317        
318         %Sort by date and time.
319         item_dates=list(:,4);
320         item_dates=cell2mat(item_dates);
321         item_dates=datenum(item_dates);
322         [item_dates,index]=sortrows(item_dates);
323         list=list(index,:);
324        
325     case{'s','sr'}
326        
327         %Sort by size
328         item_sizes=list(:,5);
329         item_sizes=cell2mat(item_sizes);
330         [item_sizes,index]=sortrows(item_sizes);
331         list=list(index,:);
332        
333 end
334
335 %--------------------------------------------------------------------------
336
337 %% Reverse the sorted order if relevant.
338
339 if numel(sort_by)==2 && sort_by(2)=='r'
340     list_items=size(list,1);
341     list=list(list_items:-1:1,:);
342 end
343  
344 %**************************************************************************
345
346 %% Group LIST as relevant.
347
348 switch group
349    
350     case{'','n'}
351        
352         %Do not group.
353        
354     case{'','d'}
355        
356         %Group directories first.
357        
358         %Determine directory indices.
359         isdir_values=list(:,6);
360         isdir_values=cell2mat(isdir_values);
361         dir_indices=find(isdir_values==1);
362
363         %Determine file indices.
364         isdir_values=list(:,6);
365         isdir_values=cell2mat(isdir_values);
366         file_indices=find(isdir_values==0);
367        
368         %Merge grouping indices.
369         regrouping_indices=[dir_indices;file_indices];
370        
371         %Group
372         list=list(regrouping_indices,:);
373        
374     case{'','f'}
375        
376         %Group files first.
377        
378         %Determine directory indices.
379         isdir_values=list(:,6);
380         isdir_values=cell2mat(isdir_values);
381         dir_indices=find(isdir_values==1);
382
383         %Determine file indices.
384         isdir_values=list(:,6);
385         isdir_values=cell2mat(isdir_values);
386         file_indices=find(isdir_values==0);
387        
388         %Merge grouping indices.
389         regrouping_indices=[file_indices;dir_indices];
390        
391         %Group
392         list=list(regrouping_indices,:);
393    
394 end
395
396 %**************************************************************************
Note: See TracBrowser for help on using the browser.