Code covered by the BSD License  

Highlights from
getfilenames.m

4.71429

4.7 | 7 ratings Rate this file 12 Downloads (last 30 days) File Size: 1.82 KB File ID: #2118

getfilenames.m

by

 

14 Aug 2002 (Updated )

Do a recursive search for files.

| Watch this File

File Information
Description

Returns a cell array of all files matching the wildcard expression "refiles" beginning in the root folder "root". Includes subdirectories.

%---Example:
mfiles = getfilenames('c:\','*.m') will return all .m files on the c drive to the cell array "mfiles".

MATLAB release MATLAB 6.1 (R12.1)
Other requirements Tested only on Win 2000
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
21 Dec 2010 Sami Kanderian

To make this code work for Matlab 2010b as well as other versions for both PC and Mac replace the lines 30 to 34:

if ispc
wildcard='*.*';
elseif ismac
wildcard='';
end

with:

wildcard='';

Now it will work with version 2010b

08 Sep 2010 K E

No longer works recursively on R2010b, even thought it does on R2010a. I am not sure why.

06 Jul 2010 K E

Useful. Wish Matlab had more builtins for file searching and management, since I write mfiles that have to be used by Windows users (no perl)

10 Jun 2010 Nitin

Works great... fast as well

25 Jun 2009 Marco

Does not work for linux, because it uses the function "ismac" instead of isunix.
Additionally, I do not see why the wildcard should be set to an empty string on a mac, a '*' would make more sense, right?

Why not use the Matlab-original function 'what'? At least for Matlab-related files, it gives you exactly what you want to do with getfilenames.

29 May 2009 Steen Johansen

Very nice. It is surprisingly fast. This will save me a lot of time.

16 Apr 2009 Shi vera

it works well in Matlab2008a(7.6).Thanks for sharing!

28 Aug 2007 Max Brugel

Best thing since sliced bread.

22 Jul 2005 Jerry Wagner

Very good. Saved me a lot of time.

15 Aug 2002 anonymous anonymous

Nice file, but one request - Can you rewrite this so that instead of CD-ing between directories, your recursive calls to DIR include the right path information? This way if the program errors or is halted (say, with cntrl-c), you don't end up in some strange directory. This would also make the program more robust to potential network problems, too (eg, such as getting "abandoned" in a stale filesystem if a network drive is suddenly disconnnected after you've CD-ed into it).

If you can do that, I'd give this file a 4 or 5!

Updates
07 Apr 2009

Removed the cd to other folders per suggestion. Now, it stays in the folder where it was called. Also added ability to simply return all subfolder names only if a single root folder argument is passed.

16 Apr 2009

Bug fix on line 36. Changed 'cd' to 'root'. Thanks Elena.

28 May 2009

Thanks to Kenn Sebesta. His Comments:

Hi, I fixed getfilenames.m to work with Mac. I'm betting it works with
Linux now, too.

The secret ingredient was `fullfile`, which lets Matlab decide for
itself how to format dir calls.

28 May 2009

Bug Fix

Contact us