📄 findfiles.m
字号:
end
% struct option argument
else
% make sure options are present
if ~isfield(opt, 'dirs')
opt.dirs = 0;
end
if ~isfield(opt, 'filesize')
opt.filesize = 0;
end
if ~isfield(opt, 'maxdepth')
if isfield(opt, 'depth')
opt.maxdepth = opt.depth;
else
opt.maxdepth = 0;
end
end
if ~isfield(opt, 'mindepth')
if isfield(opt, 'depth')
opt.mindepth = opt.depth;
else
opt.mindepth = 0;
end
end
if ~isfield(opt, 'maxage')
opt.maxage = -1;
end
if ~isfield(opt, 'minage')
opt.minage = -1;
end
if ~isfield(opt, 'oneperdir')
opt.oneperdir = 0;
end
if isfield(opt, 'rfolder')
opt = rmfield(opt, 'rfolder');
end
if ~isfield(opt, 'relative')
opt.relative = 0;
opt.rfolder = startfolder;
else
if ischar(opt.relative)
opt.rfolder=opt.relative;
opt.relative=1;
else
if double(opt.relative) >= 1
opt.rfolder = ['.' fsep];
opt.relative = 1;
else
opt.rfolder = startfolder;
opt.relative = 0;
end
end
end
if ~isfield(opt, 'return')
opt.return = 'cellarr';
end
end
end
% more interdependent checks now
if isfield(opt,'cellstr') && ...
opt.cellstr > 0
opt.return = 'cellstr';
end
if isfield(opt,'chararr') && ...
opt.chararr > 0
opt.return = 'chararr';
end
if opt.dirs ~= 0
opt.dirs = 1;
end
% check option types
if ~isa(opt.filesize, 'double')
opt.filesize = 0;
end
if ~isa(opt.maxdepth, 'double')
opt.maxdepth = 0;
end
if ~isa(opt.mindepth, 'double')
opt.mindepth = 0;
end
if ~isa(opt.maxage, 'double')
opt.maxage = -1;
end
if ~isa(opt.minage, 'double')
opt.minage = -1;
end
if opt.oneperdir ~= 0
opt.oneperdir = 1;
end
if opt.relative ~= 0
opt.relative = 1;
else opt.rfolder = startfolder;
end
% calculate age here
opt.maxage=opt.maxage / 86400;
if opt.maxage < 0
opt.maxage = -1;
end
opt.minage = opt.minage / 86400;
if opt.minage < 0
opt.minage = -1;
end
% make call for files
if opt.dirs == 0
filesfound = findsubfiles( ...
startfolder, patterns(:), 1, ...
opt.filesize, opt.mindepth, opt.maxdepth, ...
opt.minage, opt.maxage, ...
opt.oneperdir, opt.rfolder);
% make call for dirs
else
filesfound = findsubdirs( ...
startfolder, patterns(:), 1, ...
opt.mindepth, opt.maxdepth, ...
opt.minage, opt.maxage, ...
opt.oneperdir, opt.rfolder);
end
% return the correct number of values
if nargout > 1
numberfound = size(filesfound, 1);
end
% return correct type
if strcmpi(opt.return(:)', 'chararr')
filesfound = char(filesfound);
end
% - end of findfiles(...)
% %%%%internal functions%%%%
% findsubfiles
function found = findsubfiles(path, patterns, adepth, fsize, sdepth, mdepth, mnage, mxage, operdir, relative)
% start with zero files found
nfound = 0;
found = { };
mfilesep = filesep;
% first, recursively handle all subfolders, if depth is still valid
if mdepth == 0 || ...
adepth < mdepth
% get list of files and folders, and size of list
ilist = dir(path);
slist = size(ilist, 1);
% get isdir flag into array and find indices of dirs
[ilistd(1:slist)] = [ilist(:).isdir];
ilistd = find(ilistd > 0);
% check items
for count = ilistd
% don't heed . and ..
if strcmp(ilist(count).name, '.') || ...
strcmp(ilist(count).name, '..')
continue;
end;
% find files in subdirs
filestoadd = findsubfiles([path ilist(count).name mfilesep], ...
patterns, adepth + 1, fsize, sdepth, mdepth, ...
mnage, mxage, operdir, [relative ilist(count).name mfilesep]);
sfound = size(filestoadd, 1);
% if files found
if sfound > 0
nfoundfrm = nfound + 1;
nfoundnew = nfound + sfound;
found(nfoundfrm:nfoundnew) = filestoadd(:);
nfound = nfoundnew;
end
end
end
% then, if depth is valid, add files to the output
if sdepth == 0 || ...
sdepth <= adepth
% only get time if needed
if any([mnage, mxage] >= 0)
rnow = now;
end;
% number of patterns
spatt = size(patterns, 1);
for pcount = 1:spatt
% find matching entries
ilist = dir([path patterns{pcount}]);
slist = length(ilist);
% get isdir flag into array and remove dirs from list
ilistd = [];
[ilistd(1:slist)] = [ilist(:).isdir];
ilist(ilistd > 0) = [];
slist = length(ilist);
% if only one per dir
if operdir == 1
count = 1;
% reject all non-matching
while count <= slist && ...
((mnage >= 0 && (rnow - datenum(ilist(count).date)) < mnage) || ...
(mxage >= 0 && (rnow - datenum(ilist(count).date)) > mxage) || ...
(fsize ~= 0 && ilist(count).bytes ~= fsize))
count = count + 1;
end
% choose first if still valid
if count <= slist
nfound = nfound + 1;
found{nfound} = [relative ilist(count).name];
end
% otherwise check all
else
% iterate over all
for count = 1:slist
% reject non-matching
if ((mnage >= 0 && (rnow - datenum(ilist(count).date)) < mnage) || ...
(mxage >= 0 && (rnow - datenum(ilist(count).date)) > mxage) || ...
(fsize ~= 0 && ilist(count).bytes ~= fsize))
continue;
end
% accept rest
nfound = nfound + 1;
found{nfound} = [relative ilist(count).name];
end
end
end
% linearize found
found = found(:);
end
% end of function findsubfiles
% findsubdirs
function found = findsubdirs(path, patterns, adepth, sdepth, mdepth, mnage, mxage, operdir, relative)
% start with zero dirs found
nfound = 0;
found = { };
mfilesep = filesep;
% first, recursively handle all subfolders, if depth is still valid
if mdepth == 0 || ...
adepth < mdepth
% get list of files and folders, and size of list
ilist = dir(path);
slist = size(ilist, 1);
% get isdir flag into array
[ilistd(1:slist)] = [ilist(:).isdir];
% find indices of dirs
ilistd = find(ilistd > 0);
% check items
for count = ilistd
% don't heed . and ..
if strcmp(ilist(count).name, '.') || ...
strcmp(ilist(count).name, '..')
continue;
end
% iterate over subdirs
filestoadd = findsubdirs([path ilist(count).name mfilesep], ...
patterns, adepth + 1, sdepth, mdepth, ...
mnage, mxage, operdir, [relative ilist(count).name mfilesep]);
sfound = size(filestoadd, 1);
% if dirs founds
if sfound > 0
nfoundfrm = nfound + 1;
nfoundnew = nfound + sfound;
found(nfoundfrm:nfoundnew) = filestoadd(:);
nfound = nfoundnew;
end
end
end
% then, if depth is valid, add folders to the output
if sdepth == 0 || ...
sdepth <= adepth
% only get time if needed
if any([mnage, mxage]>=0)
rnow = now;
end;
% number of patterns
spatt = size(patterns, 1);
for pcount = 1:spatt
% get matching entries
ilist = dir([path patterns{pcount}]);
slist = length(ilist);
% get isdir flag into array and remove files from list
ilistd = [];
[ilistd(1:slist)] = [ilist(:).isdir];
ilist(~ilistd) = [];
slist = length(ilist);
% if only one per dir
if operdir == 1
count = 1;
% reject all non-matching entries
while count <= slist && ...
((mnage >= 0 && (rnow - datenum(ilist(count).date)) < mnage) || ...
(mxage >= 0 && (rnow - datenum(ilist(count).date)) > mxage))
count = count + 1;
end
% find next entry
while count <= slist
% still reject . and ..
if strcmp(ilist(count).name, '.') || ...
strcmp(ilist(count).name, '..')
count = count + 1;
continue;
end
% get next entry
nfound = nfound + 1;
found{nfound} = [relative ilist(count).name];
break;
end
% otherwise check all
else
% iterate over all
for count = 1:slist
% reject non-matching
if ((mnage >= 0 && (rnow - datenum(ilist(count).date)) < mnage) || ...
(mxage >= 0 && (rnow - datenum(ilist(count).date)) > mxage))
continue;
end
% reject . and ..
if strcmp(ilist(count).name, '.') || ...
strcmp(ilist(count).name, '..')
continue;
end
% accept others
nfound = nfound + 1;
found{nfound} = [relative ilist(count).name];
end
end
end
% linearize found
found = found(:);
end
% end of function findsubdirs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -