How to find files with a given pattern in multiple folders

31 次查看(过去 30 天)
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
编辑: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