Matlab查找当前目录及子目录下所有文件
来源:互联网 发布:java导出文件到桌面 编辑:程序博客网 时间:2024/05/19 20:45
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 validif ~isdir(dire) msgbox('The input isnot a valid directory','Warning','warn'); returnelseif nargin==1 ext='*';elseif nargin>2|nargin<1 msgbox('1 or 2 inputs are required','Warning','warn'); returnendif nargout>1 msgbox('Too many output arguments','Warning','warn'); returnend%containing the searching resultsD={};%create a txt file to save all the directoryfout=fopen('direc.txt','w');%containing all the directories on the same classfolder{1}=dire;flag=1; %1 when there are folders havenot be searched,0 otherwisewhile 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; endendfclose(fout);if nargout==1 directories=D';endclear D fout folder flag currfolders m files L num temp allfiles k i direcendfunction [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 countif 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); endendfilesystem=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 endendpathstruct=list(1:count);if nargin==2 listpath={pathstruct.path}; flag=regexp(listpath,syntax); pathstruct=pathstruct(~cellfun(@isempty,flag));endpathstr={pathstruct.path}';%在网上找的两段代码,第一段代码逻辑比较清楚,第二段是利用循环调用,但是persistent list count 会导致count一直在内存中存在,故每运行一次,count会一直增加
0 0
- Matlab查找当前目录及子目录下所有文件
- 查找目录下所有文件及子目录
- DIR:列出当前目录及子目录下的所有文件
- Shell编程:在当前目录下遍历所有文件和子目录及子目录下的文件
- 想要在当前目录及子目录中查找所有的‘ *.txt’文件,可以用:
- Linux查找当前目录及其子目录下所有包含指定字符串的文件
- 查找并删除当前目录及其子目录下的所有重复文件
- 对当前目录及子目录下的所有源码文件及Makefile打包
- 查找某个目录下的所有文件(包括子目录)
- 查找某个目录下的所有文件(包括子目录)
- 删除目录及目录下所有文件与子目录
- python 文件和目录 当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- 使用批处理命令移动子目录下的所有文件到当前目录下,并删除子目录
- .bat批处理 搜索当前目录及子目录下的文件
- 拷贝整个目录下所有子目录及文件的方法
- java删除目录下所有子目录及文件
- C#获取目录下及子目录所有文件
- 删除目录及子目录下所有文件【mac】
- 3.不使用(a+b)/2这种方式,求两个数的平均值。
- Dropot理解
- 理解二叉查找树
- 关于try,finally中return的顺序
- 关于OOP中集合的理解
- Matlab查找当前目录及子目录下所有文件
- editText设置背景后,被软键盘覆盖
- Linux下统计当前文件夹下的文件个数、目录个数
- HDU-1686 Oulipo(kmp模板题)
- caffe中的layers
- hdu 3842 Machine Works
- AsyncTask的用法
- 二叉树的建立与遍历
- 商人的诀窍