how to use dir in subfolders?

I am trying to search for all the xlsx files inside a folder that have many subfolders to search from. I realize matlab does not search inside subfolders. So I tried this:
>> addpath(genpath('D:/'))
>> savepath
>> dir *.xlsx
but it is not working, please advice on what I can do. I want to make a code that works for any folder that I put inside genpath(' '). Thanks!

 Accepted Answer

Image Analyst
Image Analyst on 29 Jun 2016
See the attached demo.

6 Comments

Thank you for this awesome script! I am walking through the script and get confused at this part: listOfFolderNames = {};
listOfFolderNames = [listOfFolderNames singleSubFolder];
Your code taught me a lot as I am fairly new to matlab. Do you mind explaining to me what it does? I will really really appreciate it! Thanks again :)
It just initializes listOfFolderNames to null, because if it doesn't have some value (even null), then the line where I try to concatenate:
listOfFolderNames = [listOfFolderNames singleSubFolder];
will fail because no listOfFolderNames exists yet. The only other way would be to have an "if" test where I set listOfFolderNames equal to singleSubFolder only on the first time through the loop and then the other line for the second an later passes through the loop, but that would take 5 lines of code to do all that versus just one line to initialize the listOfFolderNames to null.
That makes a lot of sense and it is amazing you can think like that. I studied through and modified your codes hoping to simplify it for a bit and use it to extract excel files, however it kept on giving me "*.xlsx not found." as results... Do you mind taking a few minutes to look at my script please? Thank you so much!
clc;
workspace;
format long g;
format compact;
start_path=matlabroot;
uiwait(msgbox('Choose your main source folder'));
mainFolder = uigetdir(start_path);
allSubFolders = genpath(mainFolder);
remain = allSubFolders;
listOfFolderNames = {};
while true
[token, remain] = strtok(remain, ';');
if isempty(token)
break;
end
listOfFolderNames = [listOfFolderNames token];
end
numFolders = length(listOfFolderNames);
for k = 1 : numFolders
lastFolder = listOfFolderNames{k};
folder = sprintf('%s/*.*', lastFolder);
dir *.xlsx
end
You're not even using folder in dir. Try replacing the last loop with this:
for k = 1 : numFolders
thisFolder = listOfFolderNames{k};
filePattern = fullfile(thisFolder, '*.xls*');
files = dir(filePattern);
if ~isempty(files)
for k2 = 1 : length(files)
fprintf('%s\n', fullfile(thisFolder, files(k2).name));
end
end
end
It works! :D I can't tell you how much I appreciate all of your help, thank you so so so much!!
Attached is a new script that works with the ** capability added in R2016b, which was released after our discussion above.
If you have R2016b or later, you should use this rather than the first script I gave in my Answer.

Sign in to comment.

More Answers (1)

Ba Mo
Ba Mo on 21 Apr 2019
i dont recommend adding folders to matlab's path, casually like this
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 *.xls'); %you can also simply write: !dir /s /b *.xls
all_mats = strsplit(all_mats,newline)';
all_mats(cellfun(@isempty,all_mats))=[]; %the last entry/line might be an empty cell. delete it.

Categories

Community Treasure Hunt

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

Start Hunting!