필터 지우기
필터 지우기

hello i want to search for ( * .inf ) files in a folder

조회 수: 3 (최근 30일)
Ishak Oussama
Ishak Oussama 2019년 1월 27일
댓글: Adam Danz 2019년 2월 7일
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

채택된 답변

Adam Danz
Adam Danz 2019년 1월 28일
편집: Adam Danz 2019년 1월 28일
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
Ishak Oussama
Ishak Oussama 2019년 2월 7일
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 2019년 2월 7일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by