Thread Subject: Q: M-file which returns file info from directories *and* sub-directories

Subject: Q: M-file which returns file info from directories *and* sub-directories

From: Cueball

Date: 12 Jan, 2005 17:04:28

Message: 1 of 5

Hi,

Has anyone taken the time to craft an M-file which returns a structure much like the command 'dir', but recurses sub-directories as well?

I came across the file 'processdir.m' at MATLAB central, but it only displays the files on-screen, it doesn't return anything...
-Mark

Subject: Q: M-file which returns file info from directories *and* sub-directories

From: Bob Gilmore

Date: 12 Jan, 2005 17:55:39

Message: 2 of 5

In article <qi7bu0ddhobcado6rbvupg0dmdrged50jd@4ax.com>,
 Cueball <mark.bray@vanderbilt.edu> wrote:

> Hi,
>
> Has anyone taken the time to craft an M-file which returns a structure much
> like the command 'dir', but recurses sub-directories as well?
>
> I came across the file 'processdir.m' at MATLAB central, but it only displays
> the files on-screen, it doesn't return anything...
> -Mark


Mark,
Check out the GENPATH command. It doesn't create a structure, but it
does give you a recursive list of directories.

You could take the results of GENAPTH and use that to make some DIR
calls. Then you can concatenate the DIR results to get a big structure.

Hope that helps,
--
Bob Gilmore, Software Engineer,
The Mathworks, Inc.

Subject: M-file which returns file info from directories *and* sub-directories

From: Ken Davis

Date: 12 Jan, 2005 17:54:19

Message: 3 of 5

"Cueball" <mark.bray@vanderbilt.edu> wrote in message
news:qi7bu0ddhobcado6rbvupg0dmdrged50jd@4ax.com...
> Hi,
>
> Has anyone taken the time to craft an M-file which returns a structure
much like the command 'dir', but recurses sub-directories as well?
>
> I came across the file 'processdir.m' at MATLAB central, but it only
displays the files on-screen, it doesn't return anything...
> -Mark

This is not like dir, but it may be useful. MATLAB's genpath does the
recursive search. It's just a matter of parsing it nicely. I think this will
work:

function t = tree(directory)
%TREE(DIRECTORY) Returns cell array list of all directories below
% DIRECTORY. If no argument is provided it returns the directories below
% the current location.
%---------------------------------------------------------------------------
-------
% !dir . /S /B /A:D % <- Note that this command only works on Windows
% platforms and can't return values. We need a
% better solution.
%---------------------------------------------------------------------------
-------
if nargin < 1
    directory = pwd;
end
t = pathlist(genpath(directory));

function p = pathlist(R)
% PATHLIST(R) Returns path R or current path as a cell array. The delimiter
% is the value returned by the pathsep function.
% Initialize to the current path.
if nargin < 1
R = path;
end
% Remove trailing path separator, if any.
if ~isempty(R)
    if isequal(R(end), pathsep)
        R = R(1:end-1);
    end
end
p = strread(R, '%s', 'delimiter', pathsep); % Use generic parse function.

Subject: Q: M-file which returns file info from directories *and* sub-directories

From: Brett Shoelson

Date: 12 Jan, 2005 22:56:36

Message: 4 of 5

Cueball wrote:
>
>
> Hi,
>
> Has anyone taken the time to craft an M-file which returns a
> structure much like the command 'dir', but recurses sub-directories
> as well?
>
> I came across the file 'processdir.m' at MATLAB central, but it
> only displays the files on-screen, it doesn't return anything...
> -Mark
>

You might want to look at FILEGREP from the FEX. It defaults to
recursive searching (including subdirectories), and can return
filenames only.
Cheers,
Brett

Subject: Q: M-file which returns file info from directories *and* sub-directories

From: Tim Farajian

Date: 12 Jan, 2005 23:05:03

Message: 5 of 5

Here is a function I had written for a previous project.

function file = subdir(cdir)
%SUBDIR List directory and all subdirectories.
% SUBDIR recursively calls itself and uses DIR to find all files
and
% directories within a specified directory and all its
subdirectories.
%
% D = SUBDIR('directory_name') returns the results in an M-by-1
% structure with the fields:
% name -- filename
% dir -- directory containing file
% date -- modification date
% bytes -- number of bytes allocated to the file
% isdir -- 1 if name is a directory and 0 if not
if nargin == 0 || isempty(cdir)
    cdir = cd; % Current directory is default
end
if cdir(end)=='\'
    cdir(end) = ''; % Remove any trailing \ from directory
end
file = dir(cdir); % Read current directory
for n = 1:length(file)
    file(n).dir = cdir; % Assign dir field
    if file(n).isdir && file(n).name(1)~='.'
        % Element is a directory -> recursively search this one
        tfile = subdir([cdir '\' file(n).name]); % Recursive call
        file = [file; tfile]; % Append to result to current directory
structure
    end
end

Tim Farajian
tfarajia@mathworks.com

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com