Expand wildcards for files and directory names

Expand wildcards for files and directory names
2K Descargas
Actualizado 4 feb 2013

Ver licencia

Nota del editor: 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

Citar como

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. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2008a
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS Linux
Categorías
Más información sobre File Operations en Help Center y MATLAB Answers.
Agradecimientos

Inspiración para: Extract fibre orientation from Moldflow

Community Treasure Hunt

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

Start Hunting!
Versión Publicado Notas de la versión
1.0.0.0