hello i want to search for * .inf files in a folder taking into account the subfolders and after listing filenames in a table column.Sans titre.png

 Réponse acceptée

Adam Danz
Adam Danz le 28 Jan 2019
Modifié(e) : Adam Danz le 28 Jan 2019
Here's a function that searches for files with a given extension. It searches through a chosen parent directory and all of its subfolder at all levels. At the end of the for-loop, you can choose wether the output (matchList) should list just the filenames or the entire path to the files. I recommend using the entire path in case you have duplicate file names.
% Name the parent directory and file extension
parentDir = 'C:\Users\foobar\Documents\MATLAB';
ext = 'inf'; %without the dot
% list all folders and subfolders
folderList = strsplit(genpath(parentDir), ';')';
% initialize loop vars
exp = sprintf('.+\\.%s', ext);
matchList = {};
% loop through each folder and search for files
for i = 1:length(folderList)
folderData = dir(folderList{i});
names = {folderData.name};
strLen = cellfun(@length, names);
endIdxCell = regexp(names, exp, 'end');
endIdx = zeros(size(endIdxCell));
endIdx(~cellfun(@isempty, endIdxCell)) = [endIdxCell{:}];
% matchList = [matchList; names(endIdx == strLen & endIdx > 0)']; % list filenames only
matchList = [matchList; fullfile(folderList{i}, names(endIdx == strLen & endIdx > 0))']; %list full paths
end

8 commentaires

Ok, then you'll use the line that is commented out instead of the line after it.
matchList = [matchList; names(endIdx == strLen & endIdx > 0)'];
thank's
Hi, I want to generalize this answer for several parentsDir
I have 20 parentDir = parentDir1,parentDir2,parentDir3...................,parentDir20
I want to calculate each matchList for each parentDir
matchList1 for parentDir1
matchList2 for parentDir2
matchList3 for parentDir3 up to matchList20 for parentDir20
thank you in advance
Adam Danz
Adam Danz le 5 Fév 2019
Modifié(e) : Adam Danz le 5 Fév 2019
Could you loop through each parent directory and apply the code to each parent directory?
Here's a slight adaptation to my solution above (UNTESTED!). "parentDir" is a cell array of strings pointing to directories. Each of those directories and all of their subdirectories will be searched.
% Name the parent directory and file extension
parentDir = {'C:\Users\foobar\Documents\MATLAB', '...', '...', 'etc...'};
ext = 'inf'; %without the dot
% initialize loop vars
exp = sprintf('.+\\.%s', ext);
matchList = {};
% list all folders and subfolders
for j = 1:length(parentDir)
folderList = strsplit(genpath(parentDir{j}), ';')';
% loop through each folder and search for files
for i = 1:length(folderList)
folderData = dir(folderList{i});
names = {folderData.name};
strLen = cellfun(@length, names);
endIdxCell = regexp(names, exp, 'end');
endIdx = zeros(size(endIdxCell));
endIdx(~cellfun(@isempty, endIdxCell)) = [endIdxCell{:}];
% matchList = [matchList; names(endIdx == strLen & endIdx > 0)']; % list filenames only
matchList = [matchList; fullfile(folderList{i}, names(endIdx == strLen & endIdx > 0))']; %list full paths
end
end
ok thanks, but i want the matchList result for each value of j
j=1 matchList=.....
j=2 matchList=.....
j=3 matchList=.....
j= length(parentDir)=.....
I want to use the result of each repertoir separately.
That's quite easy. Here's a hint.
matchList = cell(length(parentDir), 1);
for j = 1:length(parentDir)
% loop through each folder and search for files
for i = 1:length(folderList)
matchList{j}{i} = ...
end
end
matchList{1} % this is all matches for the first parent directory
matchList{2}{3} % this is the third match of the 2nd parent directory
thank you, your hint does not work ,I'm doing another hint that works very well
% Name the parent directory and file extension
parentDir ={'C:\Users\foobar\Documents\MATLAB', '...', '...', 'etc...'};
ext = 'inf'; %without the dot
% initialize loop vars
exp = sprintf('.+\\.%s', ext);
matchList = cell(length(parentDir), 1);
% list all folders and subfolders
for j = 1:length(parentDir)
folderList = strsplit(genpath(parentDir{j}), ';')';
% loop through each folder and search for files
for i = 1:length(folderList);
folderData = dir(folderList{i});
names = {folderData.name};
strLen = cellfun(@length, names);
endIdxCell = regexp(names, exp, 'end');
endIdx = zeros(size(endIdxCell));
endIdx(~cellfun(@isempty, endIdxCell)) = [endIdxCell{:}];
matchList{j} = [matchList{j}; names(endIdx == strLen & endIdx > 0)']; % list filenames only
%matchList = [matchList; fullfile(folderList{i}, names(endIdx == strLen & endIdx > 0))']; %list full paths
end
end
Adam Danz
Adam Danz le 7 Fév 2019
The hint does work if you're using it propery. It's a hint which means that the code isn't functional by itself. If you format your code so that it's more readable, I can more easily help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by