Code covered by the BSD License  

Highlights from
Files Under Folders (fuf)

4.4375

4.4 | 16 ratings Rate this file 36 Downloads (last 30 days) File Size: 3.97 KB File ID: #1378

Files Under Folders (fuf)

by Francesco di Pierro

 

22 Feb 2002 (Updated 17 Jun 2003)

Recursively search for files through directory trees under given folders.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

This function scan the folder(s) provided, and search for all files or only for those matching a wild card, specified through the * syntax.

The search can be don recursively or not and a different recursion rule can be set for each folder provided.

Depending upon an optional input argument, the function can simply return the file names or their full path.

To get the full path of a given file, FUF doesn't use the "which" command, therefore it normally works also with java function that are not loaded.

After completion, the working directory is set to the current directory at the time of the function call.

Acknowledgements
This submission has inspired the following:
Listfiles
MATLAB release MATLAB 6.0 (R12)
Other requirements Tested on 6.5 R(13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (20)
02 Apr 2004 Fahad Al Mahmood

Excellent tool indeed! Thank you so much for creating it!

10 Mar 2005 Tomasz Kawka

Excellent tool, it was very useful and simple to use. Thank you very much!

30 Mar 2005 Yogesh Mehta

Thanks, It saved me a lot of effort! Good tool.

01 Aug 2005 Yangming Ou

Great tool indeed, Very useful and convient! But I would suggest change the 'pwd' in Line236 into 'folders{i}'.

For example:
if fuf('A:\','detail') is called, the original line 236 would mistakenly outputing A:\D.xxx for some files in the subfolders (say A:\B\C\D.xxx) .

30 Aug 2005 Ink-Eyes Servant of Oni

Nice tool. I have used it efficiently on our project.

31 Aug 2005 g f

Nice tool. I have used it!

09 Sep 2005 Chad Webb

Excellent utility! I've been receiving data containing the same file name in scattered directories, but needed the full directory path and filename to each file. Until now, I had to manually construct the directory structure since there was no rhyme or reason to it. This tool fit the bill perfectly! I'll be using it a lot more in the future. Good work!

06 Jul 2006 sotirios chatzis  
10 Sep 2006 chris gotschalk

Have used this a lot. Very handy. One improvement (something like 'sortbyfilename') may be to assure that the file list returned using the 'detail' option are in the same order as that returned using 'normal'. I occasionally have had to sort these lists outside the function.

03 Jan 2007 Giuseppe L Cascella

user friendy and very effective... WELL DONE!!!

13 May 2008 Dimitri Shvorob

Great utility, but needs more work. Crashed, having attempted to 'cd' to a non-existent subfolder.

13 May 2008 Dimitri Shvorob

.. and here's the competitor that met the challenge:

http://www.mathworks.co.uk/matlabcentral/fileexchange/loadFile.do?objectId=19550&objectType=FILE

21 Aug 2009 Kenny Israni

Does this function work on the 7.8.0 (2009a) version of MATLAB?

27 Aug 2009 Aurelien Queffurust

@Kenny : yes it works perfectly in 9a within Matlab and in compiled mode (MATLAB Compiler)

I use this utility in a lot of my programs and it works as expected.

The M-Lint shows that some lines of fuf.m can be improved like:
numel(X) is usually faster than prod(size(x))
Use true or false instead of logical ...
MCC use of the CD function is problematic ...

22 Feb 2010 Seyed Iman

why I get each file name twice?
x=fuf(DynastatRoot,1,'detail');

13 Sep 2010 Aurelien Queffurust

fuf will fail with subfolders having several single dot operators like : 10.11.12
To reproduce:
>> mkdir DATA
>> cd DATA
>> mkdir 10.11.12
>> cd 10.11.12
>> srcFile = fullfile(matlabroot,'toolbox','matlab','demos','example.cdf');
>>copyfile(srcFile,'myfile.cdf');
>> cd ..
>> cd ..
>> fuf('C:\MATLAB\SOF\r2010b\bin\DATA',1,'detail')
will return
'C:\MATLAB\SOF\r2010b\bin\DATA\myfile.cdf'
instead of
 'C:\MATLAB\SOF\r2010b\bin\DATA\10.11.12\myfile.cdf'

To workaround this issue , you need to edit the rec subfunction .
I justa dded a if-test about isempty(wild):

    elseif not(strcmp(val(j).name,'.')) & not(strcmp(val(j).name,'..')) %if the jth object under the ith folder is a valid file name
            if strcmp(opt,'detail')
                % Aurelien modif 10-Sep-2010
                % test if wild empty
                if isempty(wild)
                    sorted_list =[sorted_list; cellstr(fullfile(pwd,val(j).name))];
                else
                    % case folder 20.07.2010
                    sorted_list =[sorted_list; cellstr(fullfile(pwd,wild,val(j).name))];
                end
            else
                sorted_list =[sorted_list; cellstr(val(j).name)];
            end
        end

27 Oct 2010 mathworks2011

excellent

27 Feb 2011 T. R.

Excellent tool. Simple to use and very useful.

11 Jul 2011 Daniel Higginbottom  
09 Nov 2011 Mahmoud hassan

excellent tool

Please login to add a comment or rating.
Updates
19 Mar 2002

a)The fcn can now return the mere list of file names or their full paths;
b)After completion, the working directory is set to the current directory at the time of the fcn call;
c)The fcn also handles files that are java fcns not already loaded.

23 Jul 2002

The function has been greatly enhanced. It accepts wildcards and can handle different recursion rules for each folder provided.

24 Jul 2002

Bug fixing; now it works correctly also with directories not in the Matlab search path.

17 Jun 2003

major bug fixed

Tag Activity for this File
Tag Applied By Date/Time
path Francesco di Pierro 22 Oct 2008 06:42:30
directories Francesco di Pierro 22 Oct 2008 06:42:30
files Francesco di Pierro 22 Oct 2008 06:42:30
recursive Francesco di Pierro 22 Oct 2008 06:42:30
utilities Francesco di Pierro 22 Oct 2008 06:42:30
search Francesco di Pierro 22 Oct 2008 06:42:30
potw Cristina McIntire 07 Nov 2008 13:22:22
pick of the week Jiro Doke 11 Feb 2011 20:17:48

Contact us at files@mathworks.com