2%> Find all files and directory names matching the input pattern by expanding wildcards.<br>
5%> This function performs pattern matching of file and directory names, based on wildcard characters.<br>
6%> This function is similar to wildcard expansion performed by the Unix shell
7%> and Python ``
glob.glob`` function, but it can handle by the Unix shell and
8%> Python
glob.glob function, but it can handle more types of wildcards.<br>
9%> The following
list highlights the key differences between
10%>
this function and the MATLAB intrinsic ``dir()``.<br>
12%> <li> ``
glob()`` supports wildcards
for directories.<br>
13%> <li> ``
glob()`` returns the directory part of ``pattern``.<br>
14%> <li> ``
glob()`` returns a cell array of matching names.<br>
15%> <li> ``
glob()`` does not
return hidden files and directories that
16%> start with ``
'.'`` unless explicitly specified in ``pattern``.<br>
17%> <li> ``
glob()`` does not
return ``
'.'`` and ``
'..'`` unless explicitly specified in ``pattern``.<br>
18%> <li> ``
glob()`` adds a trailing file separator to directory names.<br>
19%> <li> ``
glob()`` does not
return the contents of a directory when a directory is specified.<br>
20%> To
return contents of a directory, add a trailing ``
'/*'``.<br>
21%> <li> ``
glob()`` returns only directory names when a trailing file separator is specified.<br>
22%> <li> On Windows, ``
glob()`` is not
case sensitive, but it returns matching
23%> names exactly in the
case as they are defined on the filesystem.<br>
24%> Case of host and sharename of a UNC path and
case of drive
25%> letters will be returned as specified in ``pattern``.<br>
28%> \param[in] pattern : The input scalar MATLAB string, containing the search pattern.<br>
29%> Wildcards may be used
for basenames and
for the directory parts.<br>
30%> If pattern contains directory parts, then these will be included in the output ``pathList``.<br>
31%> Following wildcards can be used:<br>
33%> <li> ``*`` match zero or more characters.
34%> <li> ``?`` match any single character.
35%> <li> ``[ab12]`` match one of the specified characters.
36%> <li> ``[^ab12]`` match none of the specified characters
37%> <li> ``[a-z]`` match one character in range of characters
38%> <li> ``{a,b,c}`` matches any one of strings a, b or c<br>
39%> <li> All above wildcards
do not match a file separator.<br>
40%> <li> ``**`` match zero or more characters including file separators.<br>
41%> This can be used to match zero or more directory parts
42%> and will recursively
list matching names.<br>
43%> Beware that **symbolic linked directories or
44%> junctions may cause an infinite loop** when
using the ``**``.<br>
46%> \param[in] anycase : The input scalar MATLAB logical.<br>
47%> If ``
true``, the search will be
case-sensitive.<br>
48%> If ``
false``, the search will be
case-insensitive.<br>
49%> On Windows, ``anycase`` is always reset to ``
true`` even
if user-specified.<br>
50%> (**optional**.
default = ``
false`` on Unix and ``
true`` on Windows.)
53%> ``pathList`` : The output MATLAB cell array of strings containing the files
54%> or directories that match the path specified by
string ``pattern``.<br>
55%> ``isdirList`` : The output MATLAB cell array of the same size as ``pathList``,
56%> each element of
which is a MATLAB logical value that is ``
true``
if
57%> and only
if the corresponding element of ``pathList`` is a directory.<br>
62%> [pathList, isdirList] = pm.sys.path.glob(pattern)
63%> [pathList, isdirList] = pm.sys.path.glob(pattern, anycase)
70%> pm.sys.path.glob(
"*.m") %
list all .m files in current directory.
71%> pm.sys.path.glob(
"baz/*") %
list all files and directories in subdirectory
"baz".
72%> pm.sys.path.glob(
"b*/*.m") %
list all .m files in subdirectory names starting with
"b".
73%> % The
list will include the names of the matching subdirectories.
74%> pm.sys.path.glob(
"?z*.m") %
list all .m files where the second character is
'z'.
75%> pm.sys.path.glob(
"baz.[ch]") % matches baz.c and baz.h
76%> pm.sys.path.glob(
"test.[^ch]") % matches test.a but not test.c or test.h
77%> pm.sys.path.glob(
"demo.[a-c]") % matches demo.a, demo.b, and demo.c
78%> pm.sys.path.glob(
"test.{foo,bar,baz}") % matches test.foo, test.bar, and test.baz
79%> pm.sys.path.glob(
".*") %
list all hidden files in current directory, excluding
'.' and
'..'
80%> pm.sys.path.glob(
"*/") %
list all subdirectories.
81%> pm.sys.path.glob(
"**") % recursively
list all files and directories,
82%> % starting in current directory (current directory
name,
83%> % hidden files and hidden directories are
excluded).
84%> pm.sys.path.glob(
"**.m") %
list all m-files anywhere in directory tree,
85%> % including m-files in current directory. This
86%> % is equivalent with
'**/*.m'.
87%> pm.sys.path.glob(
"foo/**/") % recursively
list all directories, starting in directory
'foo'.
88%> pm.sys.path.glob(
"**/.svn/") %
list all .svn directories in directory tree.
89%> pm.sys.path.glob(
"**/.*/**") % recursively
list all files in hidden directories only.
90%> [paths, isdir] = pm.sys.path.glob(
'**'); paths(~isdir) % get all files in directory tree.
95%> \include{lineno} example/sys/path/
glob/main.m
97%> \include{lineno} example/sys/path/
glob/main.out.m
101%> Copyright (c) 2013, Peter van den Biggelaar
102%> All rights reserved.
104%> Redistribution and use in source and binary forms, with or without
105%> modification, are permitted provided that the following conditions are met:
107%> * Redistributions of source code must retain the above copyright
108%> notice,
this list of conditions and the following disclaimer.
109%> * Redistributions in binary form must reproduce the above copyright
110%> notice,
this list of conditions and the following disclaimer in
111%> the documentation and/or other materials provided with the distribution
113%> This software is provided by the copyright holders and contributors
"as is"
114%> and any express or implied warranties, including, but not limited to, the
115%> implied warranties of merchantability and fitness
for a particular purpose
116%> are disclaimed. in no
event shall the copyright owner or contributors be
117%> liable
for any direct, indirect, incidental, special, exemplary, or
118%> consequential damages (including, but not limited to, procurement of
119%> substitute goods or services; loss of use, data, or profits; or business
120%> interruption) however caused and on any theory of liability, whether in
121%> contract, strict liability, or tort (including negligence or otherwise)
122%> arising in any way out of the use of
this software, even
if advised of the
123%> possibility of such damage.
126%> \JoshuaOsborne, May 21 2024, 5:24 AM, University of Texas at Arlington<br>
127%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
128%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute
for Computational Engineering and Sciences (ICES), UT Austin<br>
129function [pathList, isdirList] =
glob(pattern, anycase)
132 pattern = convertStringsToChars(pattern);
136 %%%% check pattern input
141 %
return when pattern is empty
143 isdirList =
false(0);
145 elseif size(pattern,1)>1
146 error(
'glob:invalidInput',
'pattern must be a single string.')
149 error('
glob:invalidInput', 'pattern must be a
string.')
153 %%%% check anycase option
160 pm.introspection.
verify(anycase, "logical", 1, "anycase");
162 % Windows is not case sensitive
163 % Unix is case sensitive
168 %%%% define function handle to regular expression function for the specified case sensitivity
172 regexp_fhandle = @regexpi;
174 regexp_fhandle = @regexp;
178 %%%% only use forward slashes as file separator to prevent escaping backslashes in regular expressions
181 filespec = strrep(pattern, '\', '/');
184 %%%% split pathroot part from pattern
187 if strncmp(filespec, '
189 % pattern specifies a UNC path
190 % It is not allowed to get a directory listing of share names of a
191 % host with the DIR command.
192 % pathroot will contains e.g.
193 pathroot = regexprep(filespec, '(^
194 filespec = regexprep(filespec, '(^
196 % for Unix, multiple leading file separators are equivalent with a single file separator
197 filespec = regexprep(filespec, '^
220**', where 'a' can be any character but not '/'
222 filespec = regexprep(filespec, '([^/])(\.\*\.\*)', '$1\*/$2');
224 %%%% replace '**a' with '**
426 must also match zero directories
427 % replace '' with (|/)
428 expression = regexprep(expression, '/\.\*\.\*/', '(/\.\*\.\*/|/)');
429 % return matching names
430 if ~isempty(varargin{end})
431 % determing matching names ignoring trailing
'/'
432 L_no_trailing_fsep = regexprep(L,
'/$',
'');
433 I = regexp_fhandle(L_no_trailing_fsep, [
'^' expression
'$']);
435 % determing matching names including trailing
'/'
436 I = regexp_fhandle(L, [
'^' expression
'$']);
438 I = cellfun(
'isempty', I);
443 % determine recursive directory contents
444 % get directory contents
446 % remove hidden files
448 % only remove
'.' and
'..'
449 d(strcmp({d.name},
'.')) = [];
450 d(strcmp({d.name},
'..')) = [];
452 % remove all hidden files and directories
453 d(strncmp({d.name},
'.',1)) = [];
456 % add trailing fileseparator to directories
457 trailing_fsep = repmat({
''}, size(d));
458 trailing_fsep([d.isdir]) = {
'/'};
459 % prefix startdir to
name and postfix fileseparator
for directories
460 dname = strcat(startdir, {d.name}, trailing_fsep
');
461 [d(:).name] = deal(dname{:});
462 % recurse into subdirectories
463 for subd = {d([d.isdir]).name}
464 d = [d; dir_recur(char(subd), keep_hidden)]; %#ok<AGROW>
function verify(in varval, in vartype, in maxlen, in varname)
Verify the type and number of elements of the input varval match the specified input vartype and maxl...
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function list()
Return a list of MATLAB strings containing the names of OS platforms supported by the ParaMonte MATLA...
function glob(in pattern, in anycase)
Find all files and directory names matching the input pattern by expanding wildcards.
function glob2regexp(in glob_str)
function ls_regexp(in regexp_fhandle, in path, in varargin)
function dir_recur(in startdir, in keep_hidden)
function ls_regexp_tree(in regexp_fhandle, in path, in varargin)
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...