[37] | 1 | function [names,isdirs] = glob(pattern,prefix)
|
---|
| 2 | %GLOB Filename expansion via wildcards.
|
---|
| 3 | % GLOB(PATTERN) returns a cell array of file/directory names which match the
|
---|
| 4 | % PATTERN.
|
---|
| 5 | % [NAMES,ISDIRS] = GLOB(PATTERN) also returns a logical vector indicating
|
---|
| 6 | % which are directories.
|
---|
| 7 | %
|
---|
| 8 | % Two types of wildcards are supported:
|
---|
| 9 | % * matches zero or more characters, besides /
|
---|
| 10 | % ** matches zero or more characters, including /, ending with a /
|
---|
| 11 | % *** is interpreted as ** followed by *, which means it matches zero or
|
---|
| 12 | % more characters, including /
|
---|
| 13 | %
|
---|
| 14 | % For example, 'a*b' matches 'ab','acb','acdb', but not 'a/b'.
|
---|
| 15 | % 'a**b' matches 'ab','a/b','ac/b','ac/d/b', but not 'acb' or 'a/cb'.
|
---|
| 16 | % 'a***b' matches all of the above plus 'a/cb','ac/d/cb',etc.
|
---|
| 17 | %
|
---|
| 18 | % 'a//b' is not considered a valid filename, so 'a/*/b' will not return
|
---|
| 19 | % 'a//b' or 'a/b'.
|
---|
| 20 | %
|
---|
| 21 | % Examples:
|
---|
| 22 | % % if 'work' is a subdirectory, this returns only 'work', not its contents:
|
---|
| 23 | % glob('work')
|
---|
| 24 | % % returns 'work/fun.m' (not 'fun.m'):
|
---|
| 25 | % glob('work/fun.m')
|
---|
| 26 | % % all m-files in 'work', prefixed with 'work/':
|
---|
| 27 | % glob('work/*.m')
|
---|
| 28 | % % all files named 'fun.m' in 'work' or any subdirectory of 'work':
|
---|
| 29 | % glob('work/**fun.m')
|
---|
| 30 | % % all m-files in 'work' or any subdirectory of 'work':
|
---|
| 31 | % glob('work/***.m')
|
---|
| 32 | % % all files named 'fun.m' any subdirectory of 'work' (but not 'work'):
|
---|
| 33 | % glob('work/**/fun.m')
|
---|
| 34 | % % all files named 'fun.m' in any subdirectory of '.':
|
---|
| 35 | % glob('**fun.m')
|
---|
| 36 | % % all files in all subdirectories:
|
---|
| 37 | % glob('***')
|
---|
| 38 | %
|
---|
| 39 | % See also globstrings.
|
---|
| 40 |
|
---|
| 41 | % Written by Tom Minka, 28-Apr-2004
|
---|
| 42 | % (c) Microsoft Corporation. All rights reserved.
|
---|
| 43 |
|
---|
| 44 | if nargin < 2
|
---|
| 45 | prefix = '';
|
---|
| 46 | end
|
---|
| 47 |
|
---|
| 48 | names = {};
|
---|
| 49 | isdirs = [];
|
---|
| 50 | if isempty(pattern)
|
---|
| 51 | return
|
---|
| 52 | end
|
---|
| 53 |
|
---|
| 54 | % break the pattern into path components
|
---|
| 55 | [first,rest] = strtok(pattern,'/');
|
---|
| 56 |
|
---|
| 57 | i = strfind(first,'**');
|
---|
| 58 | if ~isempty(i)
|
---|
| 59 | % double-star pattern
|
---|
| 60 | i = i(1); % process first occurrence
|
---|
| 61 | rest = fullfile(first((i+2):end),rest);
|
---|
| 62 | first = first(1:(i-1));
|
---|
| 63 | new_pattern = [first rest];
|
---|
| 64 | % if the pattern was 'a**b/c', new_pattern is 'ab/c'
|
---|
| 65 | [names,isdirs] = glob(new_pattern,prefix);
|
---|
| 66 | first = [first '*'];
|
---|
| 67 | rest = ['**' rest];
|
---|
| 68 | % if the pattern was 'a**b/c', it is now 'a*/**b/c'
|
---|
| 69 | end
|
---|
| 70 |
|
---|
| 71 | % expand the first component
|
---|
| 72 | fullfirst = fullfile(prefix,first);
|
---|
| 73 | if ~iswild(fullfirst) & isdir(fullfirst)
|
---|
| 74 | first_files = struct('name',first,'isdir',1);
|
---|
| 75 | else
|
---|
| 76 | first_files = stripdots(dir(fullfirst));
|
---|
| 77 | end
|
---|
| 78 | % for each match, add it to the results or recurse on the rest of the pattern
|
---|
| 79 | for i = 1:length(first_files)
|
---|
| 80 | new_prefix = fullfile(prefix,first_files(i).name);
|
---|
| 81 | if isempty(rest)
|
---|
| 82 | names{end+1} = new_prefix;
|
---|
| 83 | isdirs(end+1) = first_files(i).isdir;
|
---|
| 84 | elseif first_files(i).isdir
|
---|
| 85 | [new_names, new_isdirs] = glob(rest,new_prefix);
|
---|
| 86 | names = cellcat(names, new_names);
|
---|
| 87 | isdirs = [isdirs; new_isdirs];
|
---|
| 88 | end
|
---|
| 89 | end
|
---|
| 90 | names = names(:);
|
---|
| 91 | isdirs = isdirs(:);
|
---|
| 92 |
|
---|
| 93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
| 94 |
|
---|
| 95 | function files = stripdots(files)
|
---|
| 96 | % omit . and .. from the results of DIR
|
---|
| 97 |
|
---|
| 98 | names = {files.name};
|
---|
| 99 | ok = (~strcmp(names,'.') & ~strcmp(names,'..'));
|
---|
| 100 | files = files(ok);
|
---|
| 101 |
|
---|
| 102 | function c = cellcat(c,c2)
|
---|
| 103 |
|
---|
| 104 | c = {c{:} c2{:}};
|
---|
| 105 |
|
---|
| 106 | function tf = iswild(pattern)
|
---|
| 107 |
|
---|
| 108 | tf = ~isempty(strfind(pattern,'*'));
|
---|
| 109 |
|
---|
| 110 | function s = regexp_quote(s)
|
---|
| 111 |
|
---|
| 112 | regexprep(s,'[!#]^','#^');
|
---|
| 113 | regexprep(s,'[!#]$','#$');
|
---|