How to find files with a given pattern in multiple folders

68 vues (au cours des 30 derniers jours)
012786534
012786534 le 17 Oct 2019
Modifié(e) : Adam Danz le 17 Oct 2019
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

Réponse acceptée

Adam Danz
Adam Danz le 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 commentaires
Adam Danz
Adam Danz le 17 Oct 2019
It's a well written code with informal comments!
Adam Danz
Adam Danz le 17 Oct 2019
Modifié(e) : Adam Danz le 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.

Connectez-vous pour commenter.

Plus de réponses (1)

meghannmarie
meghannmarie le 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

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by