File Exchange

image thumbnail

Expand wildcards for files and directory names

version 1.0 (6.09 KB) by

Expand wildcards for files and directory names

3 Ratings



View License

Editor's Note: 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.

    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

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

    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,, 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

    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

    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

Comments and Ratings (3)

Bob Brady

doesn't work when looking for files without extension, e.g. glob('mypath/*.')



Works great! I'm surprised that's not a built in feature in MATLAB.

MATLAB Release
MATLAB 7.6 (R2008a)

Inspired: Extract fibre orientation from Moldflow

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video