Can you use DIR to list files in subfolders ?

673 ビュー (過去 30 日間)
John
John 2012 年 3 月 12 日
編集済み: Xuelin 2024 年 1 月 23 日
Hello,
Would somebody be able to advise me on how to use DIR to find the dates that each file was last modified with a .mat extension in all sub folders in current directory?
This code does not look in sub directories. Also how to you specify the .mat extension.
files = dir(datenum)
Many thanks

採用された回答

Walter Roberson
Walter Roberson 2012 年 3 月 12 日
You cannot do that in a single dir() call.
You need one dir() call on the current folder, and you look at the isdir() field of the results to see which names correspond to folders:
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
Then do a step to remove the folder names "." and ".." so you do not infinite loop.
Then you loop over all those names and look inside each of the designated folders:
subdirinfo = cell(length(dirinfo));
for K = 1 : length(dirinfo)
thisdir = dirinfo(K).name;
subdirinfo{K} = dir(fullfile(thisdir, '*.mat'));
end
Now subdirinfo{K} is the structure of information about the .mat files in the directory dirinfo(K).name
  6 件のコメント
Vids deo
Vids deo 2016 年 8 月 9 日
Hi Walter,
I'm kinda understanding what you're saying. But please clarify if the statement means the following in words - "Assign NULL to the elements of the 'dirinfo' structure array which have '0' or FALSE in the isdir field." --Vidya
Walter Roberson
Walter Roberson 2016 年 8 月 10 日
Only if you understand that assigning NULL means to delete them.

サインインしてコメントする。

その他の回答 (6 件)

Jan
Jan 2017 年 5 月 3 日
In modern Matlab versions:
files = dir('D:\Data\**\*.mat')
  14 件のコメント
Jorge Leon
Jorge Leon 2022 年 12 月 5 日
Useful and simple. Thanks!
Xuelin
Xuelin 2024 年 1 月 23 日
編集済み: Xuelin 2024 年 1 月 23 日
Such a simple and elegant solution!

サインインしてコメントする。


Walter Roberson
Walter Roberson 2012 年 3 月 13 日
filetofind = 'data.mat';
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
tf = ismember( {dirinfo.name}, {'.', '..'});
dirinfo(tf) = []; %remove current and parent directory.
numsubdir = length(dirinfo);
lastmod = inf * ones(numsubdir,1);
for K = 1 : numsubdir
subdirinfo = dir(fullfile(dirinfo(K).name, filetofind));
if ~isempty(subdirinfo)
lastmod(K) = subdirinfo(1).datenum;
end
end
[sortedmod, sortorder] = sort(lastmod);
sordorder(~isfinite(sortedmod)) = []; %directories without data.mat
for K = 1 : length(sortorder)
thisdirnum = sortorder(K);
thisoutname = sprintf('file%d.xls', K);
%copy from the subdirectory in to a sequentially named .xls file
copy( fullfile( dirinfo(thisdirnum).name, filetofind ), thisoutname );
end
  15 件のコメント
Walter Roberson
Walter Roberson 2012 年 5 月 22 日
編集済み: Walter Roberson 2019 年 7 月 19 日
You can combine the load() into a single statement:
thisdata = load( fullfile(thisdirname,filetofind), vartofind, vartofind1 );
xlswrite( thisoutname, thisdata.(vartofind) );
xlswrite( thisoutname, thisdata.(vartofind1) );
However, you need to either write to different file names or else use range specifications on the xlswrite() -- otherwise the data from the second xlswrite() will overwrite the first.
John
John 2012 年 5 月 22 日
Thanks very much for your help. I got it working.
Regards
John

サインインしてコメントする。


Frederic Moisy
Frederic Moisy 2012 年 3 月 12 日
  1 件のコメント
John
John 2012 年 3 月 12 日
Hello Frederic,
Would you mind helping me code this for my application. I've been trying for hours with no luck.
Thank you
John

サインインしてコメントする。


Luke Melo
Luke Melo 2019 年 11 月 25 日
Found this article very useful. Here's the code I use to select a folder from a dialogue window and find all the files, png here for example, within all subdirectories:
path = uigetdir('/Users/user/')
files = dir(fullfile(path,'**','*.png'));
  4 件のコメント
Walter Roberson
Walter Roberson 2019 年 11 月 25 日
The line I show with fullfile() is an extra step to extract fully qualified file names from the structure that is returned by dir() . When the '**' wildcard is used with dir() each different result might come from a different directory, and the same name might show up with respect to different directories, so it becomes important to put together the folder name and file name.
lena kappa
lena kappa 2022 年 12 月 1 日
@Walter Roberson is there a way to save all those files in a new folder?

サインインしてコメントする。


Matthias
Matthias 2017 年 5 月 3 日
編集済み: Matthias 2017 年 5 月 3 日
Although this does not answer the whole question it adresses the title (which is what I was looking for): to get a list of all(!) subfolders below a specified folder or drive one could use the dos command like this:
[s, r] = dos('dir X:\folder\ /s /b /A:D');
folders = regexpi(r, '\n', 'split')';
Using the /s option the dos dir command will go down all the way (no option to specifiy a max recursion depth here - beware of long execution times!), with attribute D (/A:D) only directories will be returned and finally the /b option gives us a nice plain list (as a very long string) that can be easily split for further use.
Edit:
[s, r] = dos('dir X:\folder\*.mat /s /b');
matFiles = regexpi(r, '\n', 'split')';
will obviously give you the list of all .mat files, the date of these can easily be obtained with another dir() call:
l = cellfun(@dir, matFiles);
changeTimes = [l.datenum]';
  4 件のコメント
Guillaume
Guillaume 2019 年 7 月 19 日
What files? What new folder?
It doesn't look like your question has much to do with the question being answered here, so please start your own question. Give as many details as possible about what you're trying to do.
Walter Roberson
Walter Roberson 2019 年 7 月 19 日

サインインしてコメントする。


Ba Mo
Ba Mo 2019 年 4 月 21 日
as lots of users reported above, new versions of matlab support the following command dir('**/*.mat');
However, old versions of matlab don't support this
instead of writing a large code, inspect the structure field "isfield" and so on, you could just easily ask DOS (or the command prompt) to do it for you. the output from MS-DOS isn't formatted, so you need to split the one block string to separate lines
newline = char(10); %char(10) is the character for line-break, or "enter"
[~,all_mats] = system('dir /s /b *.mat'); %you can also simply write: !dir /s /b *.mat
all_mats = strsplit(all_mats,newline)';
all_mats(cellfun(@isempty,all_mats))=[]; %the last entry/line might be an empty cell. delete it.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by