5.0

5.0 | 2 ratings Rate this file 27 Downloads (last 30 days) File Size: 5.72 KB File ID: #40016

Recursive Directory Searching for Multiple File Specs

by Jonathan Sullivan

 

25 Jan 2013 (Updated 20 Feb 2013)

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
Description

 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
    subdirectories.
 
    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.
 
    EXAMPLES:
        Example 1: Recursive directory listing of the current directory
            d = dir2(pwd,'-r')
            {d.name}'
 
        Example 2: Using multiple filters
            d = dir2(pwd,'*.mat','*.m')
            {d.name}'
 
        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
            isequal(d1,d2)

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  
Everyone's Tags
dir, ext, fast, files, filespec, mex, pick of the week, potw, recursive, search
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
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

See here:
http://blogs.mathworks.com/pick/2013/02/15/recursive-directory-searching-for-multiple-file-specs-revisited/#comments

Excellent work!

Updates
28 Jan 2013

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

01 Feb 2013

Added a MEX implementation for Windows users.

05 Feb 2013

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

15 Feb 2013

Fixed minor bug in the non-recursive MEX implementation

20 Feb 2013

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

Contact us