forked from LanceOptican/lmoMatlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir2.m
204 lines (171 loc) · 4.91 KB
/
dir2.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
function dout = dir2(dn,varargin)
%DIR2 List directory.
% DIR2('directory_name') lists the files in a directory. Pathnames and
% wildcards may be used. For example, DIR *.m lists all program files
% in the current directory.
%
% DIR2('directory_name','-r') Lists the files in a directory, and it's
% subdirectories.
%
% DIR2('directory_name',filter1,filter2,...) Applies the filters FILTER1
% and FILTER2, etc to the directory search. These filters are treated as
% an OR. Thus a file must only match atleast one filter to be included.
% Filters may be strings, or cell arrays of strings.
%
% DIR2('directory_name','-r',filter1,filter2,...) The recursive flag and
% the filters may be in any order.
%
% D = DIR2('directory_name') output the results in an M-by-1
% structure with the fields:
% name -- Filename
% date -- Modification date
% bytes -- Number of bytes allocated to the file
% isdir -- 1 if name is a directory and 0 if not
% datenum -- Modification date as a MATLAB serial date number.
% This value is locale-dependent.
%
% EXAMPLES:
% Example 1: Recursive directory listing of the current directory
% d = dir2(pwd,'-r')
% {d.name}'
%
% Example 2: Using multiple filters
% d = dir2(pwd,'*.mat','*.m')
% {d.name}'
%
% Example 3: Using multiple filters w/ a recursive search
% d1 = dir2(pwd,'*.mat','*.m','-r')
% d2 = dir2(pwd,'-r','*.mat','*.m')
% % Notice order of the flags doesn't matter
% isequal(d1,d2)
%
% See also DIR, WHAT, CD, TYPE, DELETE, LS, RMDIR, MKDIR, DATENUM.
%
% By: J Sullivan
% January 2013
if ispc
if isempty(which('dir2_mex'))
install_dir2_mex;
end
if ~isempty(which('dir2_mex'))
if nargin > 0
dout = dir2_mex(dn,varargin{:});
else
dout = dir2_mex;
end
% Only print the results if nargout == 0
if nargout == 0
printAsTable({dout(:).name});
clear dout
end
return;
end
end
% Parse the inputs
if nargin == 0; dn = pwd; end
[dn, recursive, filter] = parseInputs(dn,varargin{:});
% Loop through the filters in this directory
dout = [];
for ii = 1:length(filter)
dout = [dout; dir([dn filesep filter{ii}])];
end
% Put them in alphabetical order
if ii > 1
[~,ind] = sort({dout.name});
dout = dout(ind);
end
% Remove the self and parent directory listings in recursive calls
if isRec
dout(ismember({dout.name},{'.','..'})) = [];
end
% If recursive...
if recursive
% Find all the directories
d = dir(dn);
% Remove the references to itself and its parent
d(ismember({d.name},{'.','..'})) = [];
d(~[d.isdir]) = [];
dout_rec = [];
% Loop over child directories
if ~isempty(d)
drs = strcat({dn},filesep,{d.name});
for ii = 1:length(drs)
dout_this = dir2(drs{ii},varargin{:});
if isempty(dout_this); continue; end
% Append the child directory
C = strcat(d(ii).name,filesep,{dout_this.name});
[dout_this(:).name] = deal(C{:});
dout_rec = [dout_rec; dout_this];
end
end
% Append the two sets
dout = [dout; dout_rec];
end
% Only print the results if nargout == 0
if nargout == 0
printAsTable({dout(:).name});
clear dout
end
function [dn, recursive, filter] = parseInputs(dn,varargin)
% Defaults
filter = {};
recursive = false;
% Loop over inputs
for ii = 1:length(varargin)
v = varargin{ii};
% Is it a recursive flag?
if strcmpi(v,'-r')
recursive = true;
else
% Must be a filter
if ~iscell(v)
filter = [filter {v}];
else
filter = [filter reshape(v,1,[])];
end
end
end
% If no filter specified, give them all
if isempty(filter)
filter = {'*'};
end
isStar = dn == '*';
if any(isStar)
[dn, add1, add2] = fileparts(dn);
filter = {strcat(add1,add2)};
end
function out = isRec
a = dbstack;
out = length(a) > 2 && strcmpi(a(2).name,a(3).name);
function printAsTable(C)
% Get the screen size (in charecters)
uOld = get(0,'units');
set(0,'units','characters');
sz = get(0,'CommandWindowSize');
set(0,'units',uOld);
% What's the widest?
w = sz(1);
nl = cellfun(@numel,C);
nc = numel(C);
mxl = max(nl);
mxls = num2str(mxl);
% Find out home many rows and column to take
nCol = max(floor(w./(mxl + 2)),1);
nRow = ceil(nc./nCol);
% Print it
for ii = 1:nRow
x = C(ii:nRow:end);
nThis = numel(x);
fprintf([repmat(['%-' mxls 's '],1,nThis) '\n'],x{:});
end
function install_dir2_mex
try
fileloc = which('dir2_mex.c');
curdir = pwd;
cd(fileparts(fileloc));
mex dir2_mex.c;
cd(curdir);
catch err
printf('COuld not install the mex file. Switch to the .m version\n');
warning(err.getReport);
end