Read Only files (from a directory) that contain a specific string in filename
64 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Image Analyst
on 21 Mar 2020
Edited: Image Analyst
on 21 Mar 2020
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 Comments
Image Analyst
on 22 Mar 2020
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
on 15 Aug 2023
"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');
More Answers (1)
Tom Ruopp
on 14 Aug 2023
Edited: Tom Ruopp
on 14 Aug 2023
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 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!