hello i want to search for ( * .inf ) files in a folder
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ishak Oussama
am 27 Jan. 2019
Kommentiert: Adam Danz
am 7 Feb. 2019
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/201699/Sans%20titre.png)
![Sans titre.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/201699/Sans%20titre.png)
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 28 Jan. 2019
Bearbeitet: Adam Danz
am 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 Kommentare
Adam Danz
am 7 Feb. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!