Read Only files (from a directory) that contain a specific string in filename
97 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
kounoupaki87
le 21 Mar 2020
Commenté : Stephen23
le 15 Août 2023
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 commentaires
Réponse acceptée
Image Analyst
le 21 Mar 2020
Modifié(e) : Image Analyst
le 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 commentaires
Image Analyst
le 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
le 15 Août 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');
Plus de réponses (1)
Tom Ruopp
le 14 Août 2023
Modifié(e) : Tom Ruopp
le 14 Août 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 commentaires
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!