Code covered by the BSD License  

Highlights from
Files Under Folders (fuf)

4.5

4.5 | 22 ratings Rate this file 27 Downloads (last 30 days) File Size: 3.97 KB File ID: #1378

Files Under Folders (fuf)

by

 

22 Feb 2002 (Updated )

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 file inspired Smartindent, Causal State Modeller Toolbox, Listfiles, and Mosaic Generator.

MATLAB release MATLAB 6.0 (R12)
Other requirements Tested on 6.5 R(13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (27)
11 Nov 2014 none

This is a great function, but has a major limitation due to its use of dir.m

str = 'C:\myFolder';

tic;
myFiles1 = fuf(str, 1,'detail');
t1=toc;

tic;
Folder = [str '\']; %can be a relative path
jFile = java.io.File(Folder); %java file object
%Names_Only = cellstr(char(jFile.list)); %cellstr
myFiles2 = arrayfun(@char,jFile.listFiles,'un',0); %cellstr.
t2=toc;

When the data is large in size (eg >1E5 files) or held on a network (eg cloud platform) then dir.m and hence fuf.m will fail. Use the second method to get around this:)

15 May 2014 Alexis

I only wish I'd found this 100 lines of code earlier....

16 Sep 2013 Andre  
05 Mar 2013 Nikolay S.

Excellent code, works nicely. I'd make the "reclev" input a logical instead of numeric.

25 Sep 2012 Matthieu  
10 Apr 2012 Aurelien Queffurust

@Sophia : I already reported this issue (see my above comment on 13 Sep 2010)

03 Apr 2012 Sophia

The function is of great help but has some deficiency. If the path of the folder contains dots, fuf will fail to intercept the right path. It took me a few try to figure that out. For example, fuf does not work for 'C:\...\data_03.21.2012' but 'C:\...\data_03_21_2012'

09 Nov 2011 Mahmoud hassan

excellent tool

11 Jul 2011 Daniel Higginbottom  
27 Feb 2011 T. R.

Excellent tool. Simple to use and very useful.

27 Oct 2010 mathworks2011

excellent

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

22 Feb 2010 Seyed Iman

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

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 ...

21 Aug 2009 Kenny Israni

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

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

13 May 2008 Dimitri Shvorob

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

03 Jan 2007 Giuseppe L Cascella

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

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.

06 Jul 2006 sotirios chatzis  
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!

31 Aug 2005 g f

Nice tool. I have used it!

30 Aug 2005 Ink-Eyes Servant of Oni

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

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 Mar 2005 Yogesh Mehta

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

10 Mar 2005 Tomasz Kawka

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

02 Apr 2004 Fahad Al Mahmood

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

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.

Contact us