フィルターのクリア

Read Only files (from a directory) that contain a specific string in filename

97 ビュー (過去 30 日間)
kounoupaki87
kounoupaki87 2020 年 3 月 21 日
コメント済み: Stephen23 2023 年 8 月 15 日
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.

採用された回答

Image Analyst
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
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
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
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}';

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by