How to find files with a given pattern in multiple folders

31 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 17 Oct 2019
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 Comments
Adam Danz
Adam Danz on 17 Oct 2019
Edited: Adam Danz on 17 Oct 2019
...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.

Sign in to comment.

More Answers (1)

meghannmarie
meghannmarie on 17 Oct 2019
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

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!