Home > utils > dirrec.m

dirrec

PURPOSE ^

SYNOPSIS ^

function [varargout] = dirrec(reper,ext)

DESCRIPTION ^

 Find files recursively in a given folder.

     C=dirrec('c:\windows') returns a cell C with the full pathname of all
      files in the c:\windows folder and all its sub-folders.

     C=dirrec('c:\windows','.exe') idem but returns only the files with
      extension .exe.

     C=dirrec('c:\windows','co*') idem but returns only the files starting with
      the two letters co (comsetup.log, control.ini, ...).

     C=dirrec('c:\windows',{'.exe','.dll') idem but returns files with both
      .exe and .dll extensions.

     dirrec('c:\windows','.cmd') only displays the list of the .cmd files in
      the Matlab command window

           c:\windows\system32\login.cmd
           c:\windows\system32\usrlogon.cmd

     Note that extension should be given in lower case.

     See also DIR.

     Luc Masset (2007)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [varargout] = dirrec(reper,ext)
0002 %
0003 % Find files recursively in a given folder.
0004 %
0005 %     C=dirrec('c:\windows') returns a cell C with the full pathname of all
0006 %      files in the c:\windows folder and all its sub-folders.
0007 %
0008 %     C=dirrec('c:\windows','.exe') idem but returns only the files with
0009 %      extension .exe.
0010 %
0011 %     C=dirrec('c:\windows','co*') idem but returns only the files starting with
0012 %      the two letters co (comsetup.log, control.ini, ...).
0013 %
0014 %     C=dirrec('c:\windows',{'.exe','.dll') idem but returns files with both
0015 %      .exe and .dll extensions.
0016 %
0017 %     dirrec('c:\windows','.cmd') only displays the list of the .cmd files in
0018 %      the Matlab command window
0019 %
0020 %           c:\windows\system32\login.cmd
0021 %           c:\windows\system32\usrlogon.cmd
0022 %
0023 %     Note that extension should be given in lower case.
0024 %
0025 %     See also DIR.
0026 %
0027 %     Luc Masset (2007)
0028 
0029 %  Algorithm:
0030 % The three cases - no extension, one extension and multiple extensions - are separeted to speed up the
0031 % search process. The function fileextDR replaces the fileparts function because we only need the
0032 % extension.
0033 
0034 %initialisation
0035 if nargout,
0036  varargout=[];
0037 end
0038 listF=[];
0039 
0040 %input arguments
0041 if ~nargin | nargin > 2,
0042  error('DIRREC requires 1 or two arguments')
0043  return
0044 elseif nargin == 1,
0045  ext=[];
0046 else
0047  if ~iscell(ext),
0048   if strcmpi(ext,'.*'),
0049    ext=[];
0050   end
0051  end
0052 end
0053 
0054 %list of folders
0055 listD{1,1}=reper;       % a cell containing all the searched folders
0056 indD(1)=1;              % a vector (same size as listD) indicating
0057                         % that a folder has been searched (1) or not (0)
0058 
0059 %cases
0060 if isempty(ext),
0061  icase=1;
0062 elseif ~iscell(ext),
0063  ii=strfind(ext,'*');
0064  if length(ii) == 1 & ii == length(ext);
0065   icase=2;
0066  else
0067   icase=3;
0068  end
0069 elseif iscell(ext),
0070  icase=4;
0071 else
0072  ext=[];
0073  icase=1;
0074 end
0075 
0076 %case 1: no extension given
0077 switch icase
0078 case 1,
0079  while 1,
0080   ind=find(indD);
0081   if isempty(ind),
0082    break;
0083   end
0084   ind=ind(1);
0085   rep=listD{ind};
0086   [listdir,listfile]=getdirDR1(rep);
0087   listF=[listF listfile];
0088   indD(ind)=0;
0089   nbd=length(listdir);
0090   if nbd,
0091    listD=[listD listdir];
0092    indD=[indD ones(1,nbd)];
0093   end
0094  end
0095 
0096 %case 2: start of the name given (--*)
0097 case 2,
0098  stc=strrep(ext,'*','');
0099  while 1,
0100   ind=find(indD);
0101   if isempty(ind),
0102    break;
0103   end
0104   ind=ind(1);
0105   rep=listD{ind};
0106   [listdir,listfile]=getdirDR4(rep,stc);
0107   listF=[listF listfile];
0108   indD(ind)=0;
0109   nbd=length(listdir);
0110   if nbd,
0111    listD=[listD listdir];
0112    indD=[indD ones(1,nbd)];
0113   end
0114  end
0115 
0116 %case 3: only one extension given
0117 case 3,
0118  while 1,
0119   ind=find(indD);
0120   if isempty(ind),
0121    break;
0122   end
0123   ind=ind(1);
0124   rep=listD{ind};
0125   [listdir,listfile]=getdirDR2(rep,ext);
0126   listF=[listF listfile];
0127   indD(ind)=0;
0128   nbd=length(listdir);
0129   if nbd,
0130    listD=[listD listdir];
0131    indD=[indD ones(1,nbd)];
0132   end
0133  end
0134 
0135 %case 4: several extensions given
0136 case 4,
0137  while 1,
0138   ind=find(indD);
0139   if isempty(ind),
0140    break;
0141   end
0142   ind=ind(1);
0143   rep=listD{ind};
0144   [listdir,listfile]=getdirDR3(rep,ext);
0145   listF=[listF listfile];
0146   indD(ind)=0;
0147   nbd=length(listdir);
0148   if nbd,
0149    listD=[listD listdir];
0150    indD=[indD ones(1,nbd)];
0151   end
0152  end
0153 end
0154 
0155 %display results
0156 if ~nargout,
0157  for i=1:length(listF),
0158   fprintf('%s\n',listF{i});
0159  end
0160 else
0161  varargout{1}=listF;
0162 end
0163 
0164 return
0165 
0166 %------------------------------------------------------------------------------
0167 function [listdir,listfile] = getdirDR1(reper)
0168 
0169 %dir of folder reper
0170 S=dir(reper);
0171 
0172 %separate sub-folders of reper and files
0173 n=size(S,1);
0174 listdir=cell(1,n);      % list of sub-folders
0175 listfile=cell(1,n);     % list of files
0176 for i=1:n,
0177  name=S(i).name;
0178  if S(i).isdir,
0179   if strcmp(name,'.'),  % remove current folder (.)
0180    continue;
0181   end
0182   if strcmp(name,'..'), % remove parent folder (..)
0183    continue;
0184   end
0185   listdir{i}=fullfile(reper,S(i).name);
0186  else
0187   listfile{i}=fullfile(reper,S(i).name);
0188  end
0189 end
0190 
0191 %reorder results
0192 ind=find(cellfun('isempty',listdir));
0193 listdir(ind)=[];
0194 ind=find(cellfun('isempty',listfile));
0195 listfile(ind)=[];
0196 
0197 return
0198 
0199 %------------------------------------------------------------------------------
0200 function [listdir,listfile] = getdirDR2(reper,ext)
0201 
0202 %dir of folder reper
0203 S=dir(reper);
0204 
0205 %separate sub-folders of reper and files
0206 n=size(S,1);
0207 listdir=cell(1,n);      % list of sub-folders
0208 listfile=cell(1,n);     % list of files
0209 nd=0;
0210 nf=0;
0211 for i=1:n,
0212  name=S(i).name;
0213  if S(i).isdir,
0214   if strcmp(name,'.'),  % remove current folder (.)
0215    continue;
0216   end
0217   if strcmp(name,'..'), % remove parent folder (..)
0218    continue;
0219   end
0220   nd=nd+1;
0221   listdir{nd}=fullfile(reper,S(i).name);
0222  else
0223   exte=fileextDR(name);       % compare extension
0224   if strcmpi(exte,ext),       % with given extension
0225    nf=nf+1;
0226    listfile{nf}=fullfile(reper,S(i).name);
0227   end
0228  end
0229 end
0230 
0231 %reorder results
0232 listdir(nd+1:end)=[];
0233 listfile(nf+1:end)=[];
0234 
0235 return
0236 
0237 %------------------------------------------------------------------------------
0238 function [listdir,listfile] = getdirDR3(reper,ext)
0239 
0240 %dir of folder reper
0241 S=dir(reper);
0242 
0243 %separate sub-folders of reper and files
0244 n=size(S,1);
0245 listdir=cell(1,n);      % list of sub-folders
0246 listfile=cell(1,n);     % list of files
0247 for i=1:n,
0248  name=S(i).name;
0249  if S(i).isdir,
0250   if strcmp(name,'.'),  % remove current folder (.)
0251    continue;
0252   end
0253   if strcmp(name,'..'), % remove parent folder (..)
0254    continue;
0255   end
0256   listdir{i}=fullfile(reper,S(i).name);
0257  else
0258   exte=fileextDR(name);       % extension of the file
0259   if isempty(exte),
0260    continue;
0261   end
0262   if strmatch(lower(exte),ext,'exact'),   % compare extension with given extensions
0263    listfile{i}=fullfile(reper,S(i).name);
0264   end
0265  end
0266 end
0267 
0268 %reorder results
0269 ind=find(cellfun('isempty',listdir));
0270 listdir(ind)=[];
0271 ind=find(cellfun('isempty',listfile));
0272 listfile(ind)=[];
0273 
0274 return
0275 
0276 %------------------------------------------------------------------------------
0277 function [listdir,listfile] = getdirDR4(reper,stc)
0278 
0279 %dir of folder reper
0280 S=dir(reper);
0281 
0282 %separate sub-folders of reper and files
0283 n=size(S,1);
0284 listdir=cell(1,n);      % list of sub-folders
0285 listfile=cell(1,n);     % list of files
0286 nd=0;
0287 nf=0;
0288 for i=1:n,
0289  name=S(i).name;
0290  if S(i).isdir,
0291   if strcmp(name,'.'),  % remove current folder (.)
0292    continue;
0293   end
0294   if strcmp(name,'..'), % remove parent folder (..)
0295    continue;
0296   end
0297   nd=nd+1;
0298   listdir{nd}=fullfile(reper,S(i).name);
0299  else
0300   ii=strfind(name,stc);
0301   if isempty(ii) | ii ~= 1,
0302    continue;
0303   end
0304   nf=nf+1;
0305   listfile{nf}=fullfile(reper,S(i).name);
0306  end
0307 end
0308 
0309 %reorder results
0310 listdir(nd+1:end)=[];
0311 listfile(nf+1:end)=[];
0312 
0313 return
0314 
0315 %------------------------------------------------------------------------------
0316 function [ext] = fileextDR(fname)
0317 
0318 ext=[];
0319 ind=strfind(fname,filesep);
0320 if ~isempty(ind),
0321  fname=fname(max(ind)+1:end);
0322 end
0323 ind=strfind(fname,'.');
0324 if isempty(ind),
0325  return
0326 end
0327 ext=fname(max(ind):end);
0328 
0329 return

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003