dirrec.m 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
function [varargout] = dirrec(reper,ext)
%
% 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)

% <ITA-Toolbox>
% This file is part of the application Raven for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>


%  Algorithm:
% The three cases - no extension, one extension and multiple extensions - are separeted to speed up the
% search process. The function fileextDR replaces the fileparts function because we only need the
% extension.

%initialisation
if nargout,
 varargout=[];
end
listF=[];

%input arguments
if ~nargin | nargin > 2,
 error('DIRREC requires 1 or two arguments')
 return
elseif nargin == 1,
 ext=[];
else
 if ~iscell(ext),
  if strcmpi(ext,'.*'),
   ext=[];
  end
 end
end

%list of folders
listD{1,1}=reper;       % a cell containing all the searched folders
indD(1)=1;              % a vector (same size as listD) indicating
                        % that a folder has been searched (1) or not (0)

%cases
if isempty(ext),
 icase=1;
elseif ~iscell(ext),
 ii=strfind(ext,'*');
 if length(ii) == 1 & ii == length(ext);
  icase=2;
 else
  icase=3;
 end
elseif iscell(ext),
 icase=4;
else
 ext=[];
 icase=1;
end

%case 1: no extension given
switch icase
case 1,
 while 1,
  ind=find(indD);
  if isempty(ind),
   break;
  end
  ind=ind(1);
  rep=listD{ind};
  [listdir,listfile]=getdirDR1(rep);
  listF=[listF listfile];
  indD(ind)=0;
  nbd=length(listdir);
  if nbd,
   listD=[listD listdir];
   indD=[indD ones(1,nbd)];
  end
 end

%case 2: start of the name given (--*)
case 2,
 stc=strrep(ext,'*','');
 while 1,
  ind=find(indD);
  if isempty(ind),
   break;
  end
  ind=ind(1);
  rep=listD{ind};
  [listdir,listfile]=getdirDR4(rep,stc);
  listF=[listF listfile];
  indD(ind)=0;
  nbd=length(listdir);
  if nbd,
   listD=[listD listdir];
   indD=[indD ones(1,nbd)];
  end
 end

%case 3: only one extension given
case 3,
 while 1,
  ind=find(indD);
  if isempty(ind),
   break;
  end
  ind=ind(1);
  rep=listD{ind};
  [listdir,listfile]=getdirDR2(rep,ext);
  listF=[listF listfile];
  indD(ind)=0;
  nbd=length(listdir);
  if nbd,
   listD=[listD listdir];
   indD=[indD ones(1,nbd)];
  end
 end

%case 4: several extensions given
case 4,
 while 1,
  ind=find(indD);
  if isempty(ind),
   break;
  end
  ind=ind(1);
  rep=listD{ind};
  [listdir,listfile]=getdirDR3(rep,ext);
  listF=[listF listfile];
  indD(ind)=0;
  nbd=length(listdir);
  if nbd,
   listD=[listD listdir];
   indD=[indD ones(1,nbd)];
  end
 end
end

%display results
if ~nargout,
 for i=1:length(listF),
  fprintf('%s\n',listF{i});
 end
else
 varargout{1}=listF;
end

return

%------------------------------------------------------------------------------
function [listdir,listfile] = getdirDR1(reper)

%dir of folder reper
S=dir(reper);

%separate sub-folders of reper and files
n=size(S,1);
listdir=cell(1,n);      % list of sub-folders
listfile=cell(1,n);     % list of files
for i=1:n,
 name=S(i).name;
 if S(i).isdir,
  if strcmp(name,'.'),  % remove current folder (.)
   continue;
  end
  if strcmp(name,'..'), % remove parent folder (..)
   continue;
  end
  listdir{i}=fullfile(reper,S(i).name);
 else
  listfile{i}=fullfile(reper,S(i).name);
 end
end

%reorder results
ind=find(cellfun('isempty',listdir));
listdir(ind)=[];
ind=find(cellfun('isempty',listfile));
listfile(ind)=[];

return

%------------------------------------------------------------------------------
function [listdir,listfile] = getdirDR2(reper,ext)

%dir of folder reper
S=dir(reper);

%separate sub-folders of reper and files
n=size(S,1);
listdir=cell(1,n);      % list of sub-folders
listfile=cell(1,n);     % list of files
nd=0;
nf=0;
for i=1:n,
 name=S(i).name;
 if S(i).isdir,
  if strcmp(name,'.'),  % remove current folder (.)
   continue;
  end
  if strcmp(name,'..'), % remove parent folder (..)
   continue;
  end
  nd=nd+1;
  listdir{nd}=fullfile(reper,S(i).name);
 else
  exte=fileextDR(name);       % compare extension
  if strcmpi(exte,ext),       % with given extension
   nf=nf+1;
   listfile{nf}=fullfile(reper,S(i).name);
  end
 end
end

%reorder results
listdir(nd+1:end)=[];
listfile(nf+1:end)=[];

return

%------------------------------------------------------------------------------
function [listdir,listfile] = getdirDR3(reper,ext)

%dir of folder reper
S=dir(reper);

%separate sub-folders of reper and files
n=size(S,1);
listdir=cell(1,n);      % list of sub-folders
listfile=cell(1,n);     % list of files
for i=1:n,
 name=S(i).name;
 if S(i).isdir,
  if strcmp(name,'.'),  % remove current folder (.)
   continue;
  end
  if strcmp(name,'..'), % remove parent folder (..)
   continue;
  end
  listdir{i}=fullfile(reper,S(i).name);
 else
  exte=fileextDR(name);       % extension of the file
  if isempty(exte),
   continue;
  end
  if strmatch(lower(exte),ext,'exact'),   % compare extension with given extensions
   listfile{i}=fullfile(reper,S(i).name);
  end
 end
end

%reorder results
ind=find(cellfun('isempty',listdir));
listdir(ind)=[];
ind=find(cellfun('isempty',listfile));
listfile(ind)=[];

return

%------------------------------------------------------------------------------
function [listdir,listfile] = getdirDR4(reper,stc)

%dir of folder reper
S=dir(reper);

%separate sub-folders of reper and files
n=size(S,1);
listdir=cell(1,n);      % list of sub-folders
listfile=cell(1,n);     % list of files
nd=0;
nf=0;
for i=1:n,
 name=S(i).name;
 if S(i).isdir,
  if strcmp(name,'.'),  % remove current folder (.)
   continue;
  end
  if strcmp(name,'..'), % remove parent folder (..)
   continue;
  end
  nd=nd+1;
  listdir{nd}=fullfile(reper,S(i).name);
 else
  ii=strfind(name,stc);
  if isempty(ii) | ii ~= 1,
   continue;
  end
  nf=nf+1;
  listfile{nf}=fullfile(reper,S(i).name);
 end
end

%reorder results
listdir(nd+1:end)=[];
listfile(nf+1:end)=[];

return

%------------------------------------------------------------------------------
function [ext] = fileextDR(fname)

ext=[];
ind=strfind(fname,filesep);
if ~isempty(ind),
 fname=fname(max(ind)+1:end);
end
ind=strfind(fname,'.');
if isempty(ind),
 return
end
ext=fname(max(ind):end);

return