File Exchange

image thumbnail

Files Under Folders (fuf)

version 1.0 (3.97 KB) by

Recursively search for files through directory trees under given folders.

4.54167
24 Ratings

3 Downloads

Updated

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.

Comments and Ratings (30)

Simon Tong

Worked straight out of the box ..thanks

David Smith

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

Matlab2010

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

Alexis

Alexis (view profile)

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

Andre

Andre (view profile)

Nikolay S.

Nikolay S. (view profile)

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

Matthieu

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

Sophia

Sophia (view profile)

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'

excellent tool

T. R.

T. R. (view profile)

Excellent tool. Simple to use and very useful.

mathworks2011

excellent

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

Seyed Iman

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

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

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!

Updates

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

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

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.

MATLAB Release
MATLAB 6.0 (R12)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today