How to find files with a given pattern in multiple folders

조회 수: 33 (최근 30일)
012786534
012786534 2019년 10월 17일
편집: Adam Danz 2019년 10월 17일
Hello,
I have slightly modified code shared by ImageAnalyst (I think) to built the small function below. The function below does mostly work: if I have a file named testing.txt and that I look for a file named testing.txt or even ting.txt or even .txt the function below will find the file. But not if I search for testing or test. So how can I make the function below more powerful in such a way that it will find a pattern anywhere in the file name ?
function filefinder(file, directory)
% List of the predefined directories to look into
if nargin == 1
directories = {'C:\', 'D:\', 'E:\'};
else
directories = {directory};
end
% Loop over all defined directories
for k = 1 : length(directories)
disp(sprintf('Searching: %s', directories{k}))
% Specify the file pattern.
% Note the special file pattern. It has /**/ in it to get files in subfolders of the top level folder.
filePattern = sprintf('%s/**/*%s', directories{k}, file);
allFileInfo = dir(filePattern);
% Throw out any folders. We want files only, not folders.
isFolder = [allFileInfo.isdir];
allFileInfo(isFolder) = [];
% Process all files in those folders.
totalNumberOfFiles = length(allFileInfo);
% Now we have a list of all files, matching the pattern, in the top level folder and its subfolders.
if totalNumberOfFiles >= 1
for m = 1 : totalNumberOfFiles
% Go through all those files.
thisFolder = allFileInfo(m).folder;
thisBaseFileName = allFileInfo(m).name;
fullFileName = fullfile(thisFolder, thisBaseFileName);
FileInfo = dir(fullFileName);
fprintf('Found file %d of %d : "%s". ', m, totalNumberOfFiles, fullFileName);
disp(FileInfo.date)
end
disp(' ')
else
fprintf('File not found in this folder \n');
disp(' ')
end
end
disp('Search completed !')
end

채택된 답변

Adam Danz
Adam Danz 2019년 10월 17일
Simply throw in an additional wildcard (*) here: (I've only added 1 character: *)
% Specify the file pattern.
% Note the special file pattern. It has /**/ in it to get files in subfolders of the top level folder.
filePattern = sprintf('%s/**/*%s*', directories{k}, file);
% ^ here
  댓글 수: 3
Adam Danz
Adam Danz 2019년 10월 17일
It's a well written code with informal comments!
Adam Danz
Adam Danz 2019년 10월 17일
편집: Adam Danz 2019년 10월 17일
...I just thought of a potential problem. If you're searching for something like "myFunc.m" it will also match files such as "myFunc.mat" since the wildcard will allow the extra characters following the ".m". That problem would be fairly easy to solve by the addition another line or two that enforces an extention match but it's something you should keep in mind.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

meghannmarie
meghannmarie 2019년 10월 17일
Try contains:
function filefinder(file, directory)
% List of the predefined directories to look into
if nargin == 1
directories = {'C:\', 'D:\', 'E:\'};
directories = {'D:\','D:\'};
else
directories = {directory};
end
allFileInfo = [];
for d = 1:numel(directories)
file_struct = dir(fullfile(directories{d}, '**/*'));
files = {file_struct.name};
idx = contains(files,file);
allFileInfo = [allFileInfo;file_struct(idx)];
end
totalNumberOfFiles = numel(allFileInfo);
if totalNumberOfFiles >= 1
for m = 1:totalNumberOfFiles
% Go through all those files.
thisFolder = allFileInfo(m).folder;
thisBaseFileName = allFileInfo(m).name;
fullFileName = fullfile(thisFolder, thisBaseFileName);
fprintf('Found file %d of %d : "%s". ', m, totalNumberOfFiles, fullFileName);
disp(allFileInfo(m).date)
end
disp(' ')
else
fprintf('File not found in this folder \n');
disp(' ')
end
end

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by