Code covered by the BSD License  

Highlights from
subdir: a recursive file search

4.5

4.5 | 2 ratings Rate this file 25 Downloads (last 30 days) File Size: 1.96 KB File ID: #15859

subdir: a recursive file search

by Kelly Kearney

 

07 Aug 2007 (Updated 17 Oct 2008)

Performs a recursive file search using same input and output format as dir function

| Watch this File

File Information
Description

This function performs a recursive file search. Its advantage over other FEX programs offering the same thing is that it uses the same input and output format as Matlab's dir function, and can therefore be used interchangeably.

Example:

>> a = subdir(fullfile(matlabroot, 'toolbox', 'matlab', '*.mat'))

a =

66x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

>> a(1)

ans =

       name: '/Applications/MATLAB74/toolbox/matlab/audiovideo/chirp.mat'
       date: '14-Mar-2004 10:31:48'
      bytes: 25276
      isdir: 0
    datenum: 7.3202e+05

MATLAB release MATLAB 7.1.0 (R14SP3)
Other requirements Uses anonymous functions, so requires a recent version of Matlab
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
03 Sep 2009 Raj Sodhi

It works great!.

>> a = subdir([pwd '\*level1*.csv'])

a =

19x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

My only problem when I started using it was that it really does require an extension.

>> a = subdir([pwd '\*level1*'])
??? Undefined function or variable "filter".

Error in ==> subdir at 90
    NewFiles = dir(fullfile(pathfolders{ifolder}, filter));

>>

I started troubleshooting and realized that the var filter never got defined on line 71 or 74.

Thanks for your file! Really elegant code.

yours,

Raj

23 Jul 2010 derek

There is one small issue on this file that is easily fixed. This script will only work on windows, because on line 25:
d=[d; {[rootpath '\' dnew(i).name]}];
and line 46:
d=[d; {[dtemp '\' dnew(i).name]}];

The backslash is the file separator only for window, for unix/linux this will cause problems because the file separator is '/'. This problem can easily be fixed by using filesep instead of '\'. i.e.:
d=[d; {[rootpath filesep dnew(i).name]}];
d=[d; {[dtemp filesep dnew(i).name]}];

28 Sep 2010 Kelly Kearney

Derek,

I think your comment must apply to a different submission; the lines you mention are not part of the file offered here. This version should be fully platform-independent (and was developed on a Mac, so would be Mac/Unix-biased if anything).

24 Jul 2011 Brad Stiritz

Perfect! Just what I needed. Very appreciated, thank you :)

Please login to add a comment or rating.
Updates
17 Oct 2008

Fixed small typo in description

Tag Activity for this File
Tag Applied By Date/Time
path Kelly Kearney 22 Oct 2008 09:22:22
directories Kelly Kearney 22 Oct 2008 09:22:22
files Kelly Kearney 22 Oct 2008 09:22:22
dir Kelly Kearney 22 Oct 2008 09:22:22
subdir Kelly Kearney 22 Oct 2008 09:22:22
utilities Kelly Kearney 22 Oct 2008 09:22:22
file Kelly Kearney 22 Oct 2008 09:22:22
search Kelly Kearney 22 Oct 2008 09:22:22
recursive Kelly Kearney 22 Oct 2008 09:22:22
subdir Morgan 27 Jul 2011 03:46:51

Contact us at files@mathworks.com