41 lines
1.4 KiB
Matlab
41 lines
1.4 KiB
Matlab
function fileName = findFileByCode(folderPath, code)
|
|
% FINDFILEBYCODE Searches for a file in a folder structure by a given code.
|
|
% fileName = findFileByCode(folderPath, code) searches recursively in
|
|
% folderPath for a file containing 'code' in its name and returns the
|
|
% full file name if found.
|
|
%
|
|
% Inputs:
|
|
% folderPath - The root directory to search in
|
|
% code - The unique code to search for in file names
|
|
%
|
|
% Output:
|
|
% fileName - The full file name if found, empty if not found
|
|
|
|
% Initialize output
|
|
fileName = '';
|
|
|
|
% Get list of all files and folders in the folderPath
|
|
files = dir(folderPath);
|
|
|
|
% Iterate through the list
|
|
for i = 1:length(files)
|
|
% Skip '.' and '..'
|
|
if files(i).isdir
|
|
if ~startsWith(files(i).name, '.') % Avoid hidden folders
|
|
% Recursive search in subdirectories
|
|
subFolder = fullfile(folderPath, files(i).name);
|
|
fileName = findFileByCode(subFolder, code);
|
|
if ~isempty(fileName)
|
|
return; % Stop searching if found
|
|
end
|
|
end
|
|
else
|
|
% Check if the file name contains the code
|
|
if contains(files(i).name, code)
|
|
fileName = fullfile(folderPath, files(i).name);
|
|
return;
|
|
end
|
|
end
|
|
end
|
|
end
|