File Exchange

image thumbnail

Recursive directory listing

version 1.0.0.0 (2.61 KB) by Gus Brown
Directory listing with a difference, supports directory wildcards.

2 Downloads

Updated 14 Apr 2008

View License

Lists the files in a directory and its sub directories.

Ok, so there are many functions like this one already on the file exchange, but this one uses a slightly different wildcard naming system that some people may find useful.

For example,

rdir('C:\Program Files\Windows*\**\*.dll');

will find all dll files in the directories starting with 'C:\Program Files\Windows' and all the dll files in the sub directories below them.

One can filter the list based on the file size and date.

Using the function to find files modified sometime today in the windows directory

rdir('C:\Win*\*','datenum>floor(now)');

Include the sub directories.

rdir('C:\Win*\**\*','datenum>floor(now)');

Or you could use it to find all the no zero-byte processed csv data files for the data recorded in April 2008.

rdir('data\test_200804*\csv\*.csv','bytes>0');

this finds files like...
data\test_20080401\csv\08h00.csv
data\test_20080401\csv\16h00.csv
data\test_20080402\csv\08h00.csv
data\test_20080402\csv\16h00.csv

Cite As

Gus Brown (2020). Recursive directory listing (https://www.mathworks.com/matlabcentral/fileexchange/19550-recursive-directory-listing), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (35)

SteveW

rdir Does not currently work with network shares.
Can be easily made to work with network shares by adding this to line ~100

%SW % check if network folder \\networklocation\share\
if strcmpi(name(1:2),[filesep,filesep])
p(1:3)=[];
end

Serge

Issues:
1) does not handle wrong slashes, eg rdir('A\B/C.txt') returns "A\C.txt".
2) cannot list folders.
3) Matlab's built-in also supports '**', since 2016b, and is 2x faster.

Useful features:
1) omits "." ".." from output.
2) returns full path in the name property (often used)
3) allows filtering on date and file size.

So how about this instead, best of both worlds, for 2016b onward:

function D = dirp(name,filter)
%Same as dir, plus omit '.' '..', allows filtering, returns file path.
% D = rdir(name,filter)
%DANGER: Uses eval, can execute any system command!
%Examples:
% D=dirp('**'); %all (files and folders)
% D=dirp('**\str*\**\*.str') %name filter (files and folders)
% D=dirp('**','~isdir & datenum>now-2') %files modified in last 2 days
D = dir(name); %list
D(ismember({D.name},{'.' '..'}))=[]; %remove '.' and '..'
if nargin>=2 && ~isempty(filter) %filter
bytes = [D.bytes];
isdir = [D.isdir];
datenum = [D.datenum];
try eval(sprintf('D((%s)==0)=[];',filter));
catch, error('Invalid filter "%s"',filter);
end
end
t = strcat({D.folder},filesep,{D.name}); [D.file]=t{:}; %file path

Serge

Should be part of MatLab inbuilt.

CU Chan

Recursive dir is now supported by Matlab built-in dir function since 2016b, with the new input argument support "**/" .

HX

great

Serge

Dave Ober

My mistake. Just needed tow **

Dave Ober

If I have a file
C:\Sub1\Sub2\TXTSet1.txt
And I run
cFiles = rdir('C:\Sub1\*\*\*\TxtSet*.txt')
I had hoped it would search all the sub-directories where there are stars (if they exist) in search of the file. However, it does not and requires that I know the level that the file exists. (At least that is the only way I can get it to work). However, I put my own loop around rdir and that works.

Assume!!!

Damir

No comment, it just works great.

ACLNM

Liron

Works great in Linux, exactly what I was looking for. Thank you!

Sih-Ying

One of the most useful file in daily work.

F A

Works perfectly in windows and linux

Works great. Thanks !

@ Matthias :
This is a bug of RDIR (in "if isempty(wildpath)" statement, all directories are removed, whereas it should not)

Enhanced version of RDIR I've submitted fix this bug.

See "Recursive directory listing - Enhanced RDIR" / File ID: #32226 (link above in "Acknowledgements")

I can only get files in some cases.

For example
ListOfFiles = rdir([BaseDir 'Ausleuchtung\a\' '*\**\*_ORG.tif'])

finds nothing, whereas the parent folder

ListOfFiles = rdir([BaseDir 'Ausleuchtung\' '*\**\*_ORG.tif'])

find everyting in folder \a\.

This currently makes the rdir script mostly unusable for me.

@ Pietro and other RDIR users :
I just submitted an enhanced version of RDIR function that fix bug on returned path an add some new features.

See "Recursive directory listing - Enhanced RDIR" below / File ID: #32226

pietro

Hi Thomas,so in which way can it be fixed?

@ pietro : there is indeed a bug in the way the displayed path is built in rdir function, in the "if isempty(wildpath)" statement.

Output name is built only from "prepath" and "D(ii).name" (on line 87). "postpath" is always omitted whereas it can be the parent directory name.

pietro

This function is great but unfortunately with many subfolders it has some problems. For example I have typed this:
files=rdir('C:\Users\ml\Documents\virtual\model\Multibody output signals')

and I got:
C:\Users\ml\Documents\virtual\model\Plot_1_SYSTEM;Time;;lower housing (NAUO9);xdd.txt

insted with uigetfile I got:
C:\Users\ml\Documents\virtual\model\Multibody output signals\Plot_1_SYSTEM;Time;;lower housing (NAUO9);xdd.txt

With less subfolders I don't get this problem.

pietro

The best!

A very helpful script. After some troubles in the beginning using the inconvenient double-star '**' everything is perfect now.

Kevin

Very Useful Function.
One thing I have noticed when using this function is that the results are not unique. The files that are returned seem to be repeated based on how many subdirectories deep they are. This issue is easily fixed by running unique on the results.

Oleg Komarov

Phillip Feldman

Excellent! Just what I was looking for.

The double star syntax is a bit strange. I'd like to suggest that a slightly cleaner and more flexible mechanism for controlling the recursive searching would be an optional argument that specifies the maximum folder depth of the search.

Imaging Coderguy

I used FUF extensively in the past, and though it did the job, its slowness and quirks were problematic. RDIR on the other hand is so slick and well-made that it may as well be a mathworks-made function. Great work!

Dennis Olly

FUF was great, but failed on folders with a '.' in the name, eg '\recon_P20992.7\'. Rdir is excellent!

Dimitri Shvorob

Great job! Did what neither FUF nor DIRR (earlier FEX submissions) managed to do, and surprisingly fast, too.

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