File Exchange Pick of the Week

Our best user submissions

Recursive directory searching for multiple file specs…Revisited 2

Posted by Brett Shoelson,

Brett's Pick this week is DIR2, by Jonathan Sullivan.

And now...A Recursive DIR Command that Accommodates a Complex Filterspec!

In my most recent blog post, I commented on the fact that we don't have a ready way in MATLAB of providing a complex filter specifier in a call to DIR. I laid out a list of things I'd like to be able to do with DIR, and challenged our readers to jump in the game.

And jump in you did!

I also promised to feature in my next Pick of the Week (i.e., this one), the first author and submission that met all of the criteria I laid out. The submission, I wrote, should:

  1. enable searching for multiple file formats;
  2. allow recursive or non-recursive searches;
  3. provide a command-line (non-GUI) interface;
  4. work "out-of-the-box" in R2012b--without requiring a lot of effort on my (a user's) part;
  5. return results in the same format that the dir command returns; and
  6. be shared under a BSD license.

The ink had barely dried on my Post before I received a link from Evgeny to a file that appears to meet most of my criteria. But that file is not shared on the File Exchange. And I have the sense that if I were better at understanding his instructions and writing my own regular expressions, Evgeny's dirwalk could be coerced to do what I requested. For his efforts, we'll be sending Evgeny some MATLAB swag.

Only slightly later in the afternoon, Jonathan shared his own submission to "DIR2: Recursive Directory Searching for Multiple File Specs". I was already thrilled with Jonathan's submission--he had earned this honor--when it got even better. Less than two weeks after the challenge was issued and DIR2 was uploaded, Jonathan improved his submission by converting it to MEX to speed it up. And it is fast! Very nice job, Jonathan--your file does everything I requested, and is exceptionally easy to use! For your efforts and code, an A+, a Pick-of-the-Week tag, and some cool swag coming your way!

Before I move on: Mikko, if you can show me how to get ls2 to do what I requested, I'll send you some swag, too. I tried, but can't seem to get the desired results.

As always, I welcome your thoughts and comments.

Get the MATLAB code

Published with MATLAB® R2012b


Comments are closed.

2 CommentsOldest to Newest

Mikko replied on : 1 of 2


Perhaps the reason why you’re not getting the desired results is that the program only returns plain file/dir names. I’ve now modified the source code so that the file/dir names are returned as a relative paths to the given input directory.

I also submitted the updated code to the file exchange.


Mikko replied on : 2 of 2

I made another modification. Directories are excluded from the output structure array when searching specific file specs and/or enabling file size/date filter. This will speed up the search process ~2x

I’m submitting the code to the file exchange.