View License

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

» Watch video

Highlights from
Recursive Directory Searching for Multiple File Specs

4.8 | 5 ratings Rate this file 5 Downloads (last 30 days) File Size: 5.72 KB File ID: #40016 Version: 1.7

Recursive Directory Searching for Multiple File Specs



25 Jan 2013 (Updated )

Searches a directory for multiple file specs with optional recursivity

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

 dir2 List directory.
    DIR2('directory_name') lists the files in a directory. Pathnames and
    wildcards may be used. For example, DIR *.m lists all program files
    in the current directory.
    DIR2('directory_name','-r') Lists the files in a directory, and it's
    DIR2('directory_name',filter1,filter2,...) Applies the filters FILTER1
    and FILTER2, etc to the directory search. These filters are treated as
    an OR. Thus a file must only match atleast one filter to be included.
    Filters may be strings, or cell arrays of strings.
    DIR2('directory_name','-r',filter1,filter2,...) The recursive flag and
    the filters may be in any order.
    D = DIR2('directory_name') output the results in an M-by-1
    structure with the fields:
        name -- Filename
        date -- Modification date
        bytes -- Number of bytes allocated to the file
        isdir -- 1 if name is a directory and 0 if not
        datenum -- Modification date as a MATLAB serial date number.
                   This value is locale-dependent.
        Example 1: Recursive directory listing of the current directory
            d = dir2(pwd,'-r')
        Example 2: Using multiple filters
            d = dir2(pwd,'*.mat','*.m')
        Example 3: Using multiple filters w/ a recursive search
            d1 = dir2(pwd,'*.mat','*.m','-r')
            d2 = dir2(pwd,'-r','*.mat','*.m')
            % Notice order of the flags doesn't matter

NEW: Now implemented also as MEX file. Unfortunately only windows is supported for the MEX file, but the .m version is fully function regardless of OS. The MEX version seems to provide a ~20x speedup over the .m implementation. Syntax is identical, and will mex itself (assuming you have a compiler)

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
19 Jul 2015 Serge

Serge (view profile)

This can return the same file more than once.


anything containing "test" will be picked up by both filters and one file will appear twice in results. Unique may be a simple hack fix.

Comment only
09 Apr 2014 Matt

Matt (view profile)


Comment only
09 Apr 2014 Matt

Matt (view profile)

21 Feb 2014 Michel bertrand

oups ..... sorry Jonathan, I made a mistake in my last comments; the path from the initial directory really shows up in the structure ouput by dir2; I do not know how I got to be convinced it was not the case.......Getting the fullpath would still be nice to have. Sorry again. Up-rating to 4.2 !!!

21 Feb 2014 Michel bertrand

In the current version ( feb 21st, 2014) I would rate it as 3.7;
1) I think that a default install for dir2_mex for a pc is a little too much if one just wants to try dir2 to see how it works; installing dir2_mex could be handled with a param/val list; I personally commented the ispc block so I would not have to worry about mex-ing.
2) dir2 output the files name without giving their path and therefore is of limited use in a multilevel directory situation; it can be hard to get to the file if several levels are involved
3) I tried one way to get a recursive dir on Windows using system() call to DOS dir, i.e. system( 'dir /s /b'); this however does not work with filename that contains certain characters (as in my case accented characters); dir2 works fine for such case; furthermore the dir2.m is better since it is less machine-dependent and should work equally well on Unix-like platform

10 Jul 2013 Stefan Slonevskiy

Jonathan, you might want to change the way you look for -r in the input arguments to be strncmp(temp,"-r",2)==0. The way you have it now will trigger on any path that has -r in any portion of its name.

Comment only
13 May 2013 Stefan Slonevskiy

Great tool. The speed is awesome! Thanks!! But it doesn't seem to be working with network paths (ie '\\computername\some\folder').

22 Feb 2013 Brett Shoelson

Brett Shoelson (view profile)

See here:

Excellent work!

28 Jan 2013 1.3

Fixed an issue that caused dir2 to exceed recursion limit in older versions of MATLAB.

01 Feb 2013 1.4

Added a MEX implementation for Windows users.

05 Feb 2013 1.5

Modified memory management in the MEX file to allow for a smaller footprint.

15 Feb 2013 1.6

Fixed minor bug in the non-recursive MEX implementation

20 Feb 2013 1.7

Increased the maximum number of filterspecs the MEX file can take from 20 to 10,000.

Contact us