Read Only files (from a directory) that contain a specific string in filename
조회 수: 64 (최근 30일)
이전 댓글 표시
I have a directory which contains a number of folders, each folder have data from different thermal bands. I need to read only files (from every folder in the directory) that contains B10 in the filename. Any suggestions ? On the left is the list of Folders in my directory and on the right the list of files inside every folder.
댓글 수: 0
채택된 답변
Image Analyst
2020년 3월 21일
편집: Image Analyst
2020년 3월 21일
This will do it:
folder = pwd; % The current folder, or else wherever you want.
% Get a list of all files.
fileList = dir(fullfile(folder, '*.*'));
allFileNames = {fileList.name}
% Define what pattern you will need in your filenames.
pattern = 'B10';
numFilesProcessed = 0; % For fun, let's keep track of how many files we processed.
for k = 1 : length(allFileNames)
% Get this filename.
thisFileName = fullfile(fileList(k).folder, allFileNames{k});
% See if it contains our required pattern.
if ~contains(thisFileName, pattern, 'IgnoreCase', true)
% Skip this file because the filename does not contain the required pattern.
continue;
end
% The pattern is in the filename if you get here, so do something with it.
fprintf('Now processing %s\n', thisFileName);
numFilesProcessed = numFilesProcessed + 1; % For fun, let's keep track of how many files we processed.
end
fprintf('Done running %s.m.\nWe processed %d files with %s in the name.\n', ...
mfilename, numFilesProcessed, pattern);
댓글 수: 4
Image Analyst
2020년 3월 22일
Have you considered uing two * in dir()
fileList = dir('C:/Your Parent Folder/**.txt');
or using fileDatastore()? Both of these will allow you to get all the files matching the pattern in all subfolders of some top level folder that you give it.
Stephen23
2023년 8월 15일
"Have you considered uing two * in dir() "
The DIR documentation states that "Characters next to a ** wildcard must be file separators", so the recursion pattern would have to be something like this:
fileList = dir('C:/ParentFolder/**/*.txt');
Note that if recursion is not required then one asterisk will search only the specified level of folders:
fileList = dir('C:/ParentFolder/*/*.txt');
E.g. for the folder/file hierarchy shown in the OP's question:
fileList = dir('C:/ParentFolder/*/*B10*.tif');
추가 답변 (1개)
Tom Ruopp
2023년 8월 14일
편집: Tom Ruopp
2023년 8월 14일
I just tried to do something similar in R2023a, except I was looking for the word 'Data' in my directory.
This worked for me without using a loop to get just a list of filenames that containted the string I was looking for:
currentFolder = pwd;
% Define your working folder
[myFolder] = uigetdir(currentFolder, 'Select Folder with text Files to ANALYZE');
filePattern = fullfile(myFolder, '*Data*.txt'); % Create full file name
txtFilesList = dir(filePattern); % Return list of all .txt files in chosen directory
txtFilesNames = {txtFilesList.name}';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!