Expand wildcards for files and directory names

Expand wildcards for files and directory names
2.0K 次下载
更新时间 2013/2/4

查看许可证

编者注: This file was selected as MATLAB Central Pick of the Week

Pattern matching of file and directory names, based on wildcard
characters. This function is similar to wildcard expansion performed by
the Unix shell and Python glob.glob function, but it can handle more
types of wildcards.

[LIST, ISDIR] = glob(FILESPEC)
returns cell array LIST with files or directories that match the
path specified by string FILESPEC. Wildcards may be used for
basenames and for the directory parts. If FILESPEC contains
directory parts, then these will be included in LIST.
ISDIR is a boolean, the same size as LIST that is true for
directories in LIST.

Following wildcards can be used:
* match zero or more characters
? match any single character
[ab12] match one of the specified characters
[^ab12] match none of the specified characters
[a-z] match one character in range of characters
{a,b,c} matches any one of strings a, b or c

all above wildcards do not match a file separator.

** match zero or more characters including file separators.
This can be used to match zero or more directory parts
and will recursively list matching names.

The differences between GLOB and DIR:
* GLOB supports wildcards for directories.
* GLOB returns the directory part of FILESPEC.
* GLOB returns a cell array of matching names.
* GLOB does not return hidden files and directories that start
with '.' unless explicitly specified in FILESPEC.
* GLOB does not return '.' and '..' unless explicitly specified
in FILESPEC.
* GLOB adds a trailing file separator to directory names.
* GLOB does not return the contents of a directory when
a directory is specified. To return contents of a directory,
add a trailing '/*'.
* GLOB returns only directory names when a trailing file
separator is specified.
* On Windows GLOB is not case sensitive, but it returns
matching names exactely in the case as they are defined on
the filesystem. Case of host and sharename of a UNC path and
case of drive letters will be returned as specified in
FILESPEC.

glob(FILESPEC, '-ignorecase')
Default GLOB is case sensitive on Unix. With option '-ignorecase'
FILESPEC matching is not case sensitive. On Windows, GLOB always
ignores the case. This option can be abbreviated to '-i'.

Examples:
glob *.m list all .m files in current directory.

glob baz/* list all files and directories in subdirectory 'baz'.

glob b*/*.m list all .m files in subdirectory names starting
with 'b'. The list will include the names of the
matching subdirectories.

glob ?z*.m list all .m files where the second character
is 'z'.

glob baz.[ch] matches baz.c and baz.h

glob test.[^ch] matches test.a but not test.c or test.h

glob demo.[a-c] matches demo.a, demo.b, and demo.c

glob test.{foo,bar,baz} matches test.foo, test.bar, and test.baz

glob .* list all hidden files in current directory,
excluding '.' and '..'

glob */ list all subdirectories.

glob ** recursively list all files and directories,
starting in current directory (current directory
name, hidden files and hidden directories are
excluded).

glob **.m list all m-files anywhere in directory tree,
including m-files in current directory. This
is equivalent with '**/*.m'.

glob foo/**/ recursively list all directories, starting in
directory 'foo'.

glob **/.svn/ list all .svn directories in directory tree.

glob **/.*/** recursively list all files in hidden directories
only.

[r,d]=glob('**')
r(~d) get all files in directory tree.

Known limitation:
When using '**', symbolic linked directories or junctions may cause
an infinite loop.

See also dir

引用格式

Peter van den Biggelaar (2024). Expand wildcards for files and directory names (https://www.mathworks.com/matlabcentral/fileexchange/40149-expand-wildcards-for-files-and-directory-names), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2008a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 File Operations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0