Read Only files (from a directory) that contain a specific string in filename
100 次查看(过去 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278585/image.png)
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!