Matlab查找当前目录及子目录下所有文件

时间:2023-01-11 12:07:25
function directories=findfiles(dire,ext)
%the directory you want to find files
%extension name of the files you want to find

% dire=[matlabroot,filesep,'bin\win32'];
% ext='dll';

%check if the input and output is valid
if ~isdir(dire)
msgbox('The input isnot a valid directory','Warning','warn');
return
else
if nargin==1
ext='*';
elseif nargin>2|nargin<1
msgbox('1 or 2 inputs are required','Warning','warn');
return
end
if nargout>1
msgbox('Too many output arguments','Warning','warn');
return
end

%containing the searching results
D={};

%create a txt file to save all the directory
fout=fopen('direc.txt','w');

%containing all the directories on the same class
folder{1}=dire;
flag=1; %1 when there are folders havenot be searched,0 otherwise
while flag
currfolders=folder;
folder={};

for m=1:1:length(currfolders)
direc=currfolders{m};
files=dir([direc,filesep,'*.',ext]);%当前目录下的ext文件

%the number of *.ext files in the current searching folder
L=length(files);
num=length(D);
for i=1:1:L
temp=[direc,filesep,files(i).name];
fprintf(fout,'%s\n',temp);
D{num+1}=temp;
num=num+1;
end


allfiles=dir(direc);%当前目录所有文件及子目录
%the number of all the files in the current searching folder
L=length(allfiles);
%the number of folders to be searched on this class
k=length(folder);
for i=1:1:L
if allfiles(i).isdir&(~strcmp(allfiles(i).name,'.'))&~strcmp(allfiles(i).name,'..')
k=k+1;
folder{k}=[direc,filesep,allfiles(i).name];%将所有一级子目录的目录名保存下来
end
end
end

%if there are no folders that havenot searched yet,flag=0 so the loop
%will be ended 当没有目录可遍历时,则查找结束
if ~length(folder)
flag=0;
end
end

fclose(fout);

if nargout==1
directories=D';
end
clear D fout folder flag currfolders m files L num temp allfiles k i direc

end






function [pathstruct,pathstr]=dirext(thepath,syntax)
% 使用递归方法列出或查找指定目录下的文件
% 可以使用dos命令得到相同的效果
% [~,pathstruct]=system(['dir /B/S ', thepath])
%
% 输入参数
% thepath:需要检索的目录
% syntax:匹配语法,仅支持正则匹配
% 正则语法 http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html
% .                    匹配除换行符以外的任意字符
% \w            匹配字母或数字或下划线或汉字
% \s                    匹配任意的空白符
% \d                    匹配数字
% \b                    匹配单词的开始或结束
% ^                    匹配字符串的开始
% $                    匹配字符串的结束
% *                    重复零次或更多次
% +                    重复一次或更多次
% ?                    重复零次或一次
% {n}                    重复n次
% {n,}                    重复n次或更多次
% {n,m}                    重复n到m次
%
% 输出参数
% pathstruct: 检索到的目录,结构体数组,字段同于dir函数
% pathstr: 所有文件和目录的列表
%
% 典型例子
% thepath='C:\Users\dynamic\Documents';;
% syntax='wav' % 查找所有wav文件
% [pathstruct,pathstr]=dirext(thepath,syntax)
%
persistent list count
if isempty(list)
    count=0;
    list=struct('name',[],'date',[],'bytes',[],'isdir',[],'datenum',[],'path',[]);%{thepath};
else
    tmp=length(list);
    if count>0.8*tmp;
        list(2*tmp)=list(1);
    end
end

filesystem=dir(thepath);
for i=1:length(filesystem)
    file=filesystem(i);
    name=file.name;
    type=file.isdir;
    if ~strcmpi(name,'.') && ~strcmpi(name,'..')
        count=count+1;
        nextpath=fullfile(thepath,name);
        file.path=nextpath;
        list(count)=file;
        if type
            dirext(nextpath);
        end
    end
end
pathstruct=list(1:count);
if nargin==2
    listpath={pathstruct.path};
    flag=regexp(listpath,syntax);
    pathstruct=pathstruct(~cellfun(@isempty,flag));
end
pathstr={pathstruct.path}';

%在网上找的两段代码,第一段代码逻辑比较清楚,第二段是利用循环调用,但是persistent list count 会导致count一直在内存中存在,故每运行一次,count会一直增加