Can you use DIR to list files in subfolders ?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (6)
Jan
el 3 de Mayo de 2017
In modern Matlab versions:
files = dir('D:\Data\**\*.mat')
14 comentarios
Xizhu Deng
el 18 de Mzo. de 2018
Very useful, Thanks!
Sam Walder
el 3 de Sept. de 2018
Hi Jan, I am having trouble with this. Would you expect it to work in R2016a? All the best
Youngjea
el 7 de Feb. de 2019
Great answer! Thank you Jan:)
Steven Lord
el 7 de Feb. de 2019
Sam Walder: I know this is a bit late, but no this will not work in release R2016a. The capability Jan used was introduced in release R2016b.
Lisanne Vania
el 1 de Abr. de 2019
Amazing, thank you
Coco
el 19 de Jul. de 2019
Hi Jan, Thank you. i want to put those files in the new folder, can you please help me with the coding? i am a newbby and have been tring for days with no luck.
Koustubh Shirke
el 14 de Ag. de 2019
Yes , Right it works in 2017a and onwards.
Walter Roberson
el 14 de Ag. de 2019
Koustubh Shirke it works in R2016b and onwards.
Jorge Castillo
el 9 de En. de 2020
For those using a previous version of R2016b, I propose to download the subdir function from here. Then create a script with the foll. lines and run it (F5):
a = mfilename('fullpath');
b = mfilename();
a = strrep(a,b,'');
addpath(a) % Adding the script current path
spath= 'D:\titi'; % Directory where the files are placed
list_struc = subdir(fullfile(spath, '*.c')); %Result : a List (as structure)...
% having the fullfile names of the desired file (eg. *.c)
Malte Voß
el 17 de Ag. de 2022
I have a problem doing exactly this. I'm using MATLAB R2021a and searching for specific data\**\*_blah.csv files. The file system and the matlab script is on a remote server with more thas 4000 subfolders. However only calling dir() takes so long that I aborted after half an hour. Pausing than also takes so long that I had to abort. Do you have any idea why this happens and how I can avoid it? The amount of files I am searching for is about 60 files...
Walter Roberson
el 17 de Ag. de 2022
I suggest you use the technique I show in https://www.mathworks.com/matlabcentral/answers/32038-can-you-use-dir-to-list-files-in-subfolders#answer_40544 of looping through the folders. That reduces the work to be done at any one time, so it should be less likely to freeze.
Does the remote system happen to be MacOS or Linux? If so then it might be worth using rsh or equivalent to run a remote unix find command.
Yiting Zhu
el 13 de Nov. de 2022
This solution really works!
Jorge A. Leon-Quiroga
el 5 de Dic. de 2022
Useful and simple. Thanks!
Walter Roberson
el 13 de Mzo. de 2012
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 comentarios
John
el 13 de Mzo. de 2012
Walter Roberson
el 13 de Mzo. de 2012
Sorry, should be copyfile()
John
el 13 de Mzo. de 2012
John
el 13 de Mzo. de 2012
Walter Roberson
el 13 de Mzo. de 2012
Renaming a .mat file to be a .xls file extension does not make the .mat file readable to Excel.
You have omitted describing all the steps between locating an appropriate .mat file and saving the file as .xls . .mat files may have multiple variables in different formats, and may contain objects that *cannot* be represented in Excel, so there is no universal .mat to .xls reformatter.
John
el 13 de Mzo. de 2012
Walter Roberson
el 14 de Mzo. de 2012
Editada: Walter Roberson
el 25 de Nov. de 2019
vartofind = 'ess_plant_energy_in_total_simu';
for K = 1 : length(sortorder)
thisdirnum = sortorder(K);
thisdirname = dirinfo(thisdirnum).name;
thisoutname = sprintf('file%d.xls', K);
try
thisdata = load( fullfile(thisdirname,filetofind), vartofind );
xlswrite( thisoutname, thisdata.(vartofind) );
catch
fprintf(2, 'File "%s/%s" does not have variable "%s\n", thisdirname, filetofind, vartofind);
end
end
John
el 14 de Mzo. de 2012
John
el 21 de Mayo de 2012
Editada: Walter Roberson
el 25 de Nov. de 2019
John
el 21 de Mayo de 2012
Walter Roberson
el 21 de Mayo de 2012
Only one variable can be written in one call to xlswrite(). You will need to make two xlswrite() calls with different range specifications. Unless, that is, both variables are vectors of the same size, in which case:
xlswrite(thisoutname, [thisdata.(vartofind)(:), thisdata.(vartofind1)(:)]);
Image Analyst
el 21 de Mayo de 2012
I don't know what length(sortorder) is, but if you have more than about 4 or 5 calls to xlswrite, it will take a very long time and you'd find it worthwhile to learn how to do it via ActiveX. It's not hard - I've posted demo code here before on how to do it - and it will be WAY faster since you won't have to launch and shutdown Excel each time.
John
el 22 de Mayo de 2012
Editada: Walter Roberson
el 19 de Jul. de 2019
Walter Roberson
el 22 de Mayo de 2012
Editada: Walter Roberson
el 19 de Jul. de 2019
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
el 22 de Mayo de 2012
Frederic Moisy
el 12 de Mzo. de 2012
2 votos
You can use rdir (=recursive dir), available hire: http://www.mathworks.com/matlabcentral/fileexchange/12180-fileseries-rename-renumber-files-recursive-directories
1 comentario
John
el 12 de Mzo. de 2012
Luke Melo
el 25 de Nov. de 2019
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 comentarios
Walter Roberson
el 25 de Nov. de 2019
We do, though, recommend against using path as a variable name, as path is an important MATLAB function.
Also note that files that results here will be a directory information structure. To get the file names you would use
filenames = fullfile({files.folder}, {files.name});
Luke Melo
el 25 de Nov. de 2019
interesting, I have no issues running the code without the cell {} brackets, but I'm a fan of consistent syntax
also you're right about the variable name path
Walter Roberson
el 25 de Nov. de 2019
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
el 1 de Dic. de 2022
@Walter Roberson is there a way to save all those files in a new folder?
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 comentarios
This obviously only works on Windows.
Note that since R2016b, the built-in matlab dir function supports recursive search, no need to delegate to the OS:
d = dir('X:\folder\**');
d = d([d.isdir]);
This can be (very) slow if the subfolders have a lot of files since the first line returns all files and folders.
Coco
el 19 de Jul. de 2019
Can you please help me with coding to put those files into the new folder?
thank you.
Guillaume
el 19 de Jul. de 2019
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
el 19 de Jul. de 2019
https://www.mathworks.com/matlabcentral/answers/472584-how-to-put-a-struct-files-into-a-new-folder is the relevant Question.
Ba Mo
el 21 de Abr. de 2019
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.
Categorías
Más información sobre File Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!