How to find files with a given pattern in multiple folders
33 ビュー (過去 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
0 件のコメント
採用された回答
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
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!