-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregexpdir.m
67 lines (61 loc) · 2.83 KB
/
regexpdir.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
function dirlist = regexpdir(rootdir, expstr, recursive)
% REGEXPDIR Gives a directory listing based on a regular expression
% REGEXPDIR(ROOTDIR, REGEXP) gives a directory listing of the directory
% ROOTDIR based on the pattern specified by the regular expression
% REGEXP.
%
% REGEXPDIR(ROOTDIR, REGEXP, RECURSIVE) By default REGEXPDIR traverses
% all subdirectories recursively. This behaviour can be controlled by
% supplying the optional boolean RECURSIVE. Setting this to 'false' will
% limit the function to the directory specified in ROOTDIR.
%
% Example:
% rootdir = 'C:\';
% expstr = '^.*\.exe$';
% dirlist = regexpdir(rootdir, expstr);
%
% The above example will return any EXE files on the C-drive.
%
% By default REGEXPDIR searches case insensitive. To make it case
% senstitive please specify it in the regular expression by adding
% '(?-i)' to it. Ommitting to specify the beginning '^' and ending '$'
% of the regular expression may result in unexpected behaviour.
%
% Have a look at the source code for more information. For more
% info on this function and how to use it, bug reports, feature
% requests, etc. feel free to contact the author.
%
% See also DIR, REGEXP, REGEXPTRANSLATE
%==========================================================================
% B.C. Hamans (b.c.hamans@rad.umcn.nl) 2007
%==========================================================================
% Check input arguments
error(nargchk(2, 3, nargin));
if ~exist('recursive','var'); recursive = true; end
% Check if the root directory contains a trailing file seperator or supply
rootdir = char(rootdir);
if ~strcmp(rootdir(length(rootdir)), filesep); rootdir=[rootdir filesep]; end
% Remember initial starting directory when traversing.
global basedir; if isempty(basedir); basedir = rootdir; end % changed persistent to global (BB)
% Traverse the structure
dirlist = cell({}); dirtree = dir(rootdir);
for h = find([dirtree.isdir]);
if ~any(strcmp({'.','..'}, dirtree(h).name));
if regexpi(strrep(fullfile(rootdir, dirtree(h).name, filesep), basedir, ''), expstr);
dirlist = [dirlist; fullfile(rootdir, dirtree(h).name, filesep)];
end
if recursive;
dirlist = [dirlist; regexpdir(fullfile(rootdir, dirtree(h).name, filesep), expstr, recursive)];
end
end
end
for i = find(~[dirtree.isdir]);
if regexpi(strrep(fullfile(rootdir, dirtree(i).name), basedir, ''), expstr);
dirlist = [dirlist; fullfile(rootdir, dirtree(i).name)];
end
end
%==========================================================================
% Changelog:
% 03-09-2007 v1.00 (BCH) Initial release
% 20-09-2007 v1.01 (BCH) Proper solution for persistent variable 'basedir'
%==========================================================================