File Exchange

image thumbnail

Files Under Folders (fuf)

version (3.97 KB) by Francesco di Pierro
Recursively search for files through directory trees under given folders.


Updated 17 Jun 2003

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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.

Cite As

Francesco di Pierro (2021). Files Under Folders (fuf) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (30)

Simon Tong

Worked straight out of the box ..thanks

Afshin Salehi

David Smith

This is a great function. Wish I found it earlier.


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

str = 'C:\myFolder';

myFiles1 = fuf(str, 1,'detail');

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

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:)


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


Nikolay S.

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


Aurelien Queffurust

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


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'

Mahmoud hassan

excellent tool

Daniel Higginbottom

T. R.

Excellent tool. Simple to use and very useful.



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');
>> cd ..
>> cd ..
>> fuf('C:\MATLAB\SOF\r2010b\bin\DATA',1,'detail')
will return
instead of

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))];
% case folder 20.07.2010
sorted_list =[sorted_list; cellstr(fullfile(pwd,wild,val(j).name))];
sorted_list =[sorted_list; cellstr(val(j).name)];

Seyed Iman

why I get each file name twice?

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

Kenny Israni

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

Dimitri Shvorob

Dimitri Shvorob

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

Giuseppe L Cascella

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

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.

sotirios chatzis

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!

g f

Nice tool. I have used it!

Ink-Eyes Servant of Oni

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

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:\ for some files in the subfolders (say A:\B\C\ .

Yogesh Mehta

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

Tomasz Kawka

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

Fahad Al Mahmood

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

MATLAB Release Compatibility
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!