-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubdir.m
151 lines (133 loc) · 3.79 KB
/
subdir.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
function varargout = subdir(varargin)
%SUBDIR Performs a recursive file search
%
% subdir
% subdir(name)
% files = subdir(...)
%
% This function performs a recursive file search. The input and output
% format is identical to the dir function.
%
% Input variables:
%
% name: pathname or filename for search, can be absolute or relative
% and wildcards (*) are allowed. If ommitted, the files in the
% current working directory and its child folders are returned
%
% Output variables:
%
% files: m x 1 structure with the following fields:
% name: full filename
% date: modification date timestamp
% bytes: number of bytes allocated to the file
% isdir: 1 if name is a directory; 0 if no
%
% Example:
%
% >> a = subdir(fullfile(matlabroot, 'toolbox', 'matlab', '*.mat'))
%
% a =
%
% 67x1 struct array with fields:
% name
% date
% bytes
% isdir
%
% >> a(2)
%
% ans =
%
% name: '/Applications/MATLAB73/toolbox/matlab/audiovideo/chirp.mat'
% date: '14-Mar-2004 07:31:48'
% bytes: 25276
% isdir: 0
%
% See also:
%
% dir
% Copyright 2006 Kelly Kearney
%---------------------------
% Get folder and filter
%---------------------------
narginchk(0,1);
nargoutchk(0,1);
if nargin == 0
folder = pwd;
filter = '*';
else
[folder, name, ext] = fileparts(varargin{1});
if isempty(folder)
folder = pwd;
end
if isempty(ext)
if isdir(fullfile(folder, name))
folder = fullfile(folder, name);
filter = '*';
else
filter = [name ext];
end
else
filter = [name ext];
end
if ~isdir(folder)
error('Folder (%s) not found', folder);
end
end
%---------------------------
% Search all folders
%---------------------------
pathstr = genpath_local(folder);
pathfolders = regexp(pathstr, pathsep, 'split'); % Same as strsplit without the error checking
pathfolders = pathfolders(~cellfun('isempty', pathfolders)); % Remove any empty cells
Files = [];
pathandfilt = fullfile(pathfolders, filter);
for ifolder = 1:length(pathandfilt)
NewFiles = dir(pathandfilt{ifolder});
if ~isempty(NewFiles)
fullnames = cellfun(@(a) fullfile(pathfolders{ifolder}, a), {NewFiles.name}, 'UniformOutput', false);
[NewFiles.name] = deal(fullnames{:});
Files = [Files; NewFiles];
end
end
%---------------------------
% Prune . and ..
%---------------------------
if ~isempty(Files)
[~, ~, tail] = cellfun(@fileparts, {Files(:).name}, 'UniformOutput', false);
dottest = cellfun(@(x) isempty(regexp(x, '\.+(\w+$)', 'once')), tail);
Files(dottest & [Files(:).isdir]) = [];
end
%---------------------------
% Output
%---------------------------
if nargout == 0
if ~isempty(Files)
fprintf('\n');
fprintf('%s\n', Files.name);
fprintf('\n');
end
elseif nargout == 1
varargout{1} = Files;
end
function [p] = genpath_local(d)
% Modified genpath that doesn't ignore:
% - Folders named 'private'
% - MATLAB class folders (folder name starts with '@')
% - MATLAB package folders (folder name starts with '+')
files = dir(d);
if isempty(files)
return
end
p = ''; % Initialize output
% Add d to the path even if it is empty.
p = [p d pathsep];
% Set logical vector for subdirectory entries in d
isdir = logical(cat(1,files.isdir));
dirs = files(isdir); % Select only directory entries from the current listing
for i=1:length(dirs)
dirname = dirs(i).name;
if ~strcmp( dirname,'.') && ~strcmp( dirname,'..')
p = [p genpath(fullfile(d,dirname))]; % Recursive calling of this function.
end
end